问题描述
我试图了解异步和等待是如何如何工作的。在程序中如何控制出行。这里是code这点我是想明白了。
i am trying to understand how how Async and Await works. how control travel in the program. here is the code which i was trying to understand.
public async Task MyMethod()
{
Task<int> longRunningTask = LongRunningOperation();
//indeed you can do independent to the int result work here
MySynchronousMethod();
//and now we call await on the task
int result = await longRunningTask;
//use the result
Console.WriteLine(result);
}
public async Task<int> LongRunningOperation() // assume we return an int from this long running operation
{
await Task.Delay(5000); //5 seconds delay
return 1;
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
MyMethod();
}
当点击按钮会出现那么的MyMethod()将被调用,并从的MyMethod LongRunningOperation()将被调用,它需要5秒才能完成。所以我的问题是
when button click occur then MyMethod() will be called and from the MyMethod LongRunningOperation() will be called and it take 5 sec to complete. so my question is
这是什么线的意思
Task<int> longRunningTask = LongRunningOperation();
我觉得LongRunningOperation()函数将被调用。
i think LongRunningOperation() function will be called.
再有什么这样做 INT结果=等待longRunningTask;
以上行可以致力于一线我们可以构建像
任务&LT; INT&GT; longRunningTask =等待LongRunningOperation();
或
the above line could be committed and one line we can construct like Task<int> longRunningTask = await LongRunningOperation();
or
INT结果=等待longRunningTask;
所以会发生什么。
1)如果longRunningOperation尚未完成,目前仍在运行的MyMethod()将返回到它的调用方法,从而在主线程不被封锁。当longRunningOperation完成然后一个线程从线程池(可以是任何螺纹)将在其previous状态返回的MyMethod(),并继续执行(在这种情况下打印结果到控制台)。
1) if the longRunningOperation hasn't finished and 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() at its previous state and continue execution (in this case printing the result to the console).
一个第二壳体是,该longRunningOperation已经完成其执行,其结果是可用的。当到达的await longRunningOperation编译器知道它的结果,将保持在非常相同的线程上执行code。 (在这种情况下,打印结果控制台)。
A second case would be that the longRunningOperation has already finished its execution and the result is available. When reaching the await longRunningOperation the compiler knows that it has the result and will keep on executing code on the very same thread. (in this case printing result to console).
1点是并不清楚对我来说,语句如果longRunningOperation尚未完成,目前仍在运行的MyMethod()将返回到它的调用方法
point 1 is not at all clear to me like the statement "if the longRunningOperation hasn't finished and is still running, MyMethod() will return to its calling method
"
如果可能更详细地解释该点之一。谢谢
if possible explain the point one in more detail. thanks
推荐答案
我有一个我的博客上,你会发现有用的。
I have an async
intro on my blog that you may find helpful.
这code:
int result = await LongRunningOperation();
基本上相同,因为这code:
is essentially the same as this code:
Task<int> resultTask = LongRunningOperation();
int result = await resultTask;
所以,是的, LongRunningOperation
由该方法直接调用。
在该等待
经营者传递一个已经完成的任务,这将提取结果,并继续执行此方法(同步)。
When the await
operator is passed an already-completed task, it will extract the result and continue executing the method (synchronously).
在该等待
经营者传递了一个不完整的任务(例如,通过 LongRunningOperation
返回的任务将是不完整的),则默认为等待
将捕获当前上下文,并从方法返回一个不完整的任务。
When the await
operator is passed an incomplete task (e.g., the task returned by LongRunningOperation
will not be complete), then by default await
will capture the current context and return an incomplete task from the method.
后来,等待
任务完成时,该方法的其余部分计划在该环境中运行。
Later, when the await
task completes, the remainder of the method is scheduled to run in that context.
这个背景是 SynchronizationContext.Current
,除非它是空
,在这种情况下,它是 TaskScheduler.Current
。如果你在一个控制台应用程序运行此,那么上下文通常是线程池的范围内,因此异步
方法将恢复线程池线程执行。但是,如果你在UI线程执行同一方法,那么上下文是一个UI上下文和异步
方法将恢复UI线程上执行。
This "context" is SynchronizationContext.Current
unless it is null
, in which case it is TaskScheduler.Current
. If you're running this in a Console app, then the context is usually the thread pool context, so the async
method will resume executing on a thread pool thread. However, if you execute the same method on a UI thread, then the context is a UI context and the async
method will resume executing on the UI thread.
这篇关于如何异步和等待的作品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!