相关疑难解决方法(0)

正确使用Task.Run时和async-await时

我想问你关于正确架构何时使用的意见Task.Run.我在WPF .NET 4.5应用程序(使用Caliburn Micro框架)中遇到了滞后的UI.

基本上我在做(非常简化的代码片段):

public class PageViewModel : IHandle<SomeMessage>
{
   ...

   public async void Handle(SomeMessage message)
   {
      ShowLoadingAnimation();

      // Makes UI very laggy, but still not dead
      await this.contentLoader.LoadContentAsync();

      HideLoadingAnimation();
   }
}

public class ContentLoader
{
    public async Task LoadContentAsync()
    {
        await DoCpuBoundWorkAsync();
        await DoIoBoundWorkAsync();
        await DoCpuBoundWorkAsync();

        // I am not really sure what all I can consider as CPU bound as slowing down the UI
        await DoSomeOtherWorkAsync();
    }
}
Run Code Online (Sandbox Code Playgroud)

从我阅读/看到的文章/视频中,我知道await async不一定在后台线程上运行,并且需要在后台开始工作,需要等待它Task.Run(async () => …

c# asynchronous task async-await

288
推荐指数
2
解决办法
25万
查看次数

在C#中的控制台应用程序异步?

我有这个简单的代码:

public static async Task<int> SumTwoOperationsAsync()
{
    var firstTask = GetOperationOneAsync();
    var secondTask = GetOperationTwoAsync();
    return await firstTask + await secondTask;
}


private async Task<int> GetOperationOneAsync()
{
    await Task.Delay(500); // Just to simulate an operation taking time
    return 10;
}

private async Task<int> GetOperationTwoAsync()
{
    await Task.Delay(100); // Just to simulate an operation taking time
    return 5;
}
Run Code Online (Sandbox Code Playgroud)

大.这个编译.

但是让我们说我有一个控制台应用程序,我想运行上面的代码(调用SumTwoOperationsAsync())

 static  void Main(string[] args)
        {
             SumTwoOperationsAsync();
        }
Run Code Online (Sandbox Code Playgroud)

但我读过,(使用时sync),我不得不一路同步向上向下 :

问题:这是否意味着我的Main功能应该标记为 …

c# async-await c#-5.0 .net-4.5

168
推荐指数
2
解决办法
10万
查看次数

如何异步Files.ReadAllLines并等待结果?

我有以下代码,

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        button1.IsEnabled = false;

        var s = File.ReadAllLines("Words.txt").ToList(); // my WPF app hangs here
        // do something with s

        button1.IsEnabled = true;
    }
Run Code Online (Sandbox Code Playgroud)

Words.txt有很多单词,我读入s变量,我试图利用C#5中的asyncawait关键字,Async CTP Library所以WPF应用程序不会挂起.到目前为止,我有以下代码,

    private async void button1_Click(object sender, RoutedEventArgs e)
    {
        button1.IsEnabled = false;

        Task<string[]> ws = Task.Factory.FromAsync<string[]>(
            // What do i have here? there are so many overloads
            ); // is this the right way to do?

        var s = await File.ReadAllLines("Words.txt").ToList(); …
Run Code Online (Sandbox Code Playgroud)

.net c# asynchronous async-await c#-5.0

62
推荐指数
1
解决办法
4万
查看次数

为什么有些异步方法需要返回类型的Task,而有些则不需要

在Microsoft的此示例中,该方法的返回类型为Task<int>

例1:

async Task<int> AccessTheWebAsync()
{ 
    // You need to add a reference to System.Net.Http to declare client.
    HttpClient client = new HttpClient();

    // GetStringAsync returns a Task<string>. That means that when you await the 
    // task you'll get a string (urlContents).
    Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");

    // You can do work here that doesn't rely on the string from GetStringAsync.
    DoIndependentWork();

    // The await operator suspends AccessTheWebAsync. 
    //  - AccessTheWebAsync can't continue until getStringTask is complete. 
    // …
Run Code Online (Sandbox Code Playgroud)

c# asynchronous task-parallel-library async-await

1
推荐指数
1
解决办法
3170
查看次数