简体   繁体   English

在c#5.0中,“async / await”函数是否始终在运行开始时在主线程上运行

[英]In c# 5.0, does “async/await” function always run on main thread at the beginning of running

I find that when I call an "async" function, at first, the thread is the main thread. 我发现当我调用“异步”函数时,首先,线程是主线程。 When it meets the await Task, then the thread will be changed to another one. 当它遇到await Task时,该线程将被更改为另一个。

static void Main(string[] args)
{
    println("Mainthread ID " + Thread.CurrentThread.GetHashCode());
    var s = DoSomethingAsync();
    println("Waiting for DoSomethingAsync Complete ");
    Thread.Sleep(2000);
    println(s.Result.ToString());
}

static async Task<int> DoSomethingAsync()
{
    println("DoSomethingAsync Thread ID " + Thread.CurrentThread.GetHashCode());
    int val = await Task.Factory.StartNew(() =>
    {
        Thread.Sleep(1000);
        println("Task Thread ID " + Thread.CurrentThread.GetHashCode());
        return 100;
    });

    println("DoSomethingAsync end " + Thread.CurrentThread.GetHashCode());
    return val;

} }

And the result is as follow: 结果如下:

11:40:34 383 Mainthread ID 1
11:40:34 398 DoSomethingAsync Thread ID 1
11:40:34 400 Waiting for DoSomethingAsync Complete
11:40:35 400 Task Thread ID 3
11:40:35 400 DoSomethingAsync end 3
11:40:36 401 100

We can see the mainthread id is 1. DoSomethingAsync Thread ID is also 1. They are same. 我们可以看到mainthread id是1.DoSomethingAsync线程ID也是1.它们是相同的。 So at the beginning of running "async" function, it's the main thread that do this part. 所以在运行“async”函数之初,它就是执行此部分的主线程。

This means when the main thread meet the "async" function, it will be blocked to run something until it meet the await Task.The "async" function doesn't make things asynchronous instantly. 这意味着当主线程遇到“异步”函数时,它将被阻塞以运行某些东西,直到它遇到await Task。“async”函数不会立即异步。 Is it right? 这样对吗?

I know that if there's no await Task in the "async" function ,this "async" function will be run synchronously. 我知道如果在“异步”函数中没有等待任务,那么这个“异步”函数将同步运行。

This means when the main thread meet the "async" function, it will be blocked to run something until it meet the await Task.The "async" function doesn't make things asynchronous instantly. 这意味着当主线程遇到“异步”函数时,它将被阻塞以运行某些东西,直到它遇到await Task。“async”函数不会立即异步。 Is it right? 这样对吗?

Yes, that is right. 是的,这是正确的。 Your async method will run until hitting its first await keyword, which will yield control back to the caller after that. 您的async方法将一直运行,直到命中它的第一个await关键字,然后在此之后将控制权返回给调用者。 In your example, you don't await on that method, so it will continue to execute the next method right away. 在您的示例中,您没有await该方法,因此它将继续立即执行下一个方法。

As a side note, a Console Application doesn't really have a Main Thread , as it uses the default ThreadPoolSynchronizationContext . 作为旁注,控制台应用程序实际上没有主线程 ,因为它使用默认的ThreadPoolSynchronizationContext That means that you'll see any arbitrary thread id after your awaited async method. 这意味着在等待的异步方法之后,您将看到任意线程ID。

The method will start running in whatever thread calls the method. 该方法将在调用该方法的任何线程中开始运行。 If the method is called from the main thread, it'll start running in the main thread. 如果从主线程调用该方法,它将开始在主线程中运行。 If it's called from a thread pool thread it'll start running in a thread pool thread. 如果从线程池线程调用它,它将开始在线程池线程中运行。 It is no different from any other method in that regard. 在这方面,它与任何其他方法没有什么不同。

Continuations after any await operation will leverage the current SyncrhonizationContext to determine how they are scheduled. 任何await操作之后的连续将利用当前的SyncrhonizationContext来确定它们的调度方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM