CodeGo.net>如何模拟一个昂贵的异步操作?
作者:互联网
this page中的代码真的是模拟长时间运行任务的最佳方法吗?
我研究过的控制台应用程序非常简单,并且看起来还不错.
我不确定是否可以将DoExpensiveCalculation换成Async方法,例如来自HttpClient的GetStringAsync而不出现问题.
using System;
using System.Threading.Tasks;
namespace ExpensiveAsync
{
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("started");
var t = CalculateResult("stuff and that");
Console.WriteLine("press return");
Console.ReadLine();
}
public static async Task<string> CalculateResult(string data)
{
// This queues up the work on the threadpool.
var expensiveResultTask = Task.Run(() => DoExpensiveCalculation(data));
// Note that at this point, you can do some other work concurrently,
// as CalculateResult() is still executing!
Console.WriteLine("concurrent");
// Execution of CalculateResult is yielded here!
var result = await expensiveResultTask; // CalculateResult returns the Task object here
Console.WriteLine("synchronous"); // this code runs once the DoExpensiveCalculation method has finished
return result;
}
public static string DoExpensiveCalculation(string data)
{
var completionTime = DateTime.Now.AddSeconds(3);
Console.WriteLine("begin");
while (DateTime.Now < completionTime) ;
Console.WriteLine("finish");
return data;
}
}
}
解决方法:
该代码的结构意味着DoExpensiveCalculation是CPU绑定的操作.如果要在不占用CPU的情况下进行CPU绑定的操作,则Thread.Sleep是合适的选项.
I’m not sure if I could swap out DoExpensiveCalculation for an an Async method, like GetStringAsync from HttpClient without issue.
好吧,那是完全不同的. GetStringAsync不是CPU绑定的操作;这是一个受I / O约束的操作.
I / O绑定操作自然是异步的,可以直接与async和await一起使用.与CPU绑定的操作自然是同步的,因此有时您需要使用Task.Run.在这种情况下,“有时”通常表示“当您在UI线程上时”,以便在执行CPU绑定操作时不会冻结UI.当将CPU绑定的操作包装在Task.Run中时,可以将其视为异步,即从UI线程等待它.
您的示例代码是一个控制台应用程序,通常不需要使用Task.Run.控制台应用程序通常运行它们需要运行和退出的任何代码;在大多数情况下,不需要保持响应状态的UI,因此不需要Task.Run.
标签:net-core,async-await,console-application,c 来源: https://codeday.me/bug/20191108/2006892.html