在哪里“期待已久的”任务执行? [英] Where do 'awaited' tasks execute?

查看:77
本文介绍了在哪里“期待已久的”任务执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下几点:

 专用异步无效btnSlowPoke_Click(对象发件人,EventArgs的发送)
{
    等待DoItAsync();
}私人异步任务< INT> SomeLongJobAsync()
{
    对于(INT X = 0; X< 999999; X ++)
    {
        //思考我的存在一秒
        等待Task.Delay(1000);
    }
    返回42;
}公共异步任务< INT> DoItAsync()
{
    Console.Write(她会来圆山);
    任务< INT> T = SomeLongJobAsync(); //< - 在什么线程中执行这个?
    Console.WriteLine(当她来了。);
    返回的await吨;
}


  1. 第一次写入DoItAsync()执行。

  2. SomeLongJobAsync()开始。

  3. 的WriteLine DoItAsync()执行。

  4. DoItAsync()暂停而 SomeLongJobAsync()直到把它完成工作了。

  5. SomeLongJobAsync()完成,因此 DoItAsync()的回报。

同时,UI响应。

在什么线程中 SomeLongJobAsync()执行?


解决方案

简答

这是异步的方法由 GUI 线程发射将执行同一线程上,只要有CPU运算的执行。其他异步方法开始调用线程上运行,并继续在线程池主题。

长的答案

SomeLongJobAsync 调用线程,直到它到达(即印刷的她会来圆山的一个)上开始执行一个等待。那么任务返回重新presents异步操作+之后的延续。当的全部的操作完成任务将完成(除非它完成prematurely因异常或取消)。

Task.Delay(1000)本身是执行的没有线程的,因为需要没有。最后 Task.Delay(1000)结束的时候,一个线程的的恢复所需要。哪一个线程是依赖于 的SynchronizationContext (默认情况下存在的的所以线程是线程池线程,但在 GUI 应用它的烧毛GUI线程,多这里)。该线程,直到它到达另一个异步点执行code的剩余部分(即另一个等待),等等等等。

Consider the following:

private async void btnSlowPoke_Click(object sender, EventArgs e)
{
    await DoItAsync();
}

private async Task<int> SomeLongJobAsync()
{
    for (int x = 0; x < 999999; x++)
    {
        //ponder my existence for one second
        await Task.Delay(1000);
    }
    return 42;
}

public async Task<int> DoItAsync()
{
    Console.Write("She'll be coming round the mountain");
    Task<int> t = SomeLongJobAsync();  //<--On what thread does this execute?
    Console.WriteLine(" when she comes.");
    return await t;
}

  1. The first Write in DoItAsync() executes.
  2. SomeLongJobAsync() starts.
  3. The WriteLine in DoItAsync() executes.
  4. DoItAsync() pauses while SomeLongJobAsync() works away until it's done.
  5. SomeLongJobAsync() completes, so DoItAsync() returns.

Meanwhile, the UI is responsive.

On what thread does SomeLongJobAsync() execute?

解决方案

Short Answer

An async method fired by the GUI thread will execute on the same thread, whenever there are CPU operation to execute. Other async methods start running on the calling thread and continue on a ThreadPool thread.

Long Answer

SomeLongJobAsync starts executing on the calling thread (the one that printed "She'll be coming round the mountain") up until it reaches an await. Then a task is returned that represents the asynchronous operation + the continuation after it. When the entire operation is done the task will complete (unless it completes prematurely due to an exception or cancellation).

When Task.Delay(1000) itself is "executing" there is no thread, because none is needed. And when finally Task.Delay(1000) ends, a thread is needed to resume on. Which thread it is depends on the SynchronizationContext (by default there is none so the thread is a ThreadPool thread, but in a GUI application it's the singe GUI thread, more here). That thread executes the rest of the code until it reaches another asynchronous point (i.e. another await) and so forth and so forth.

这篇关于在哪里“期待已久的”任务执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆