如何以及何时使用`async`和`await` [英] how to and when use `async` and `await`
问题描述
这是我的理解主要事情一个异步
和等待
做的是让code容易写入和读取 - 但使用它们等于产卵后台线程来执行长时间的逻辑
目前,我正在尝试最基本的例子。我行内添加了一些意见。你能澄清一下吗?
//我不uderstand为什么这个方法必须标记为异步。
私人异步无效的button1_Click(对象发件人,EventArgs的)
{
任务< INT>访问= DoSomethingAsync();
//任务的独立这里的东西
//此行达到后5秒内从休眠
// DoSomethingAsync()方法。难道不应该立即联系?
INT一个= 1;
//从我的理解等待应该在这里完成。
INT X =等待机会;
}
异步任务< INT> DoSomethingAsync()
{
//是在后台线程执行这个?
System.Threading.Thread.Sleep(5000);
返回1;
}
在使用异步
和等待
编译器产生一个状态机的背景。
下面是关于这一点我希望我能解释一些高层次的细节是怎么回事的例子:
公共异步任务MyMethodAsync()
{
任务< INT> longRunningTask = LongRunningOperationAsync();
//其不需要LongRunningOperationAsync的结果独立工作可以在这里完成
//现在我们叫等待的任务
INT结果=等待longRunningTask;
//使用结果
Console.WriteLine(结果);
}
公共异步任务< INT> LongRunningOperationAsync()//假设我们返回一个int从这个长期运行的操作
{
等待Task.Delay(1000); // 1秒延时
返回1;
}
好了,所以这里会发生什么:
-
任务< INT> longRunningTask = LongRunningOperationAsync();
开始执行LongRunningOperation -
独立工作完成后,让我们假设主线程(线程ID = 1),那么
等待longRunningOperation
为止。
现在,如果longRunningOperation尚未完成,仍在运行,的MyMethod()
将返回到它的调用方法,因此,主线程不会被封锁。当 longRunningOperation
完成然后从线程池线程(可以是任何线程)将返回的MyMethod()
的其previous上下文并继续执行(在这种情况下打印结果到控制台)。
第二种情况是,该 longRunningOperation
已完成其执行,其结果是可用的。当到达计谋longRunningOperation
我们已经有了结果,因此code将继续执行上非常相同的线程。 (在这种情况下,打印结果到控制台)。当然,这不是上面例子中的情况下,如果有一个 Task.Delay(1000)
参与。
From my understanding one of the main things that async
and await
do is to make code easy to write and read - but is using them equal to spawning background threads to perform long duration logic?
I'm currently trying out the most basic example. I've added some comments inline. Can you clarify it for me?
// i don't uderstand why this method must be marked as "async'.
private async void button1_Click(object sender, EventArgs e)
{
Task<int> access = DoSomethingAsync();
// task independent stuff here
// this line is reached after the 5 seconds sleep from
// DoSomethingAsync() method. Shouldn't it be reached immediately?
int a = 1;
// from my understanding the waiting should be done here.
int x = await access;
}
async Task<int> DoSomethingAsync()
{
// is this executed on a background thread?
System.Threading.Thread.Sleep(5000);
return 1;
}
When using async
and await
the compiler generates a state machine in the background.
Here's an example on which I hope I can explain some of the high-level details that are going on:
public async Task MyMethodAsync()
{
Task<int> longRunningTask = LongRunningOperationAsync();
// independent work which doesn't need the result of LongRunningOperationAsync can be done here
//and now we call await on the task
int result = await longRunningTask;
//use the result
Console.WriteLine(result);
}
public async Task<int> LongRunningOperationAsync() // assume we return an int from this long running operation
{
await Task.Delay(1000); //1 seconds delay
return 1;
}
Ok, so what happens here:
Task<int> longRunningTask = LongRunningOperationAsync();
starts executing LongRunningOperationindependent work is done on let's assume the Main Thread (Thread ID = 1) then
await longRunningOperation
is reached.
Now, if the longRunningOperation hasn't finished and it is still running, MyMethod()
will return to its calling method, thus the main thread doesn't get blocked. When the longRunningOperation
is done then a thread from the ThreadPool (can be any thread) will return to MyMethod()
in its previous context and continue execution (in this case printing the result to the console).
A second case would be that the longRunningOperation
has already finished its execution and the result is available. When reaching the await longRunningOperation
we already have the result so the code will continue executing on the very same thread. (in this case printing result to console). Of course this is not the case for the above example, where there's a Task.Delay(1000)
involved.
这篇关于如何以及何时使用`async`和`await`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!