简体   繁体   English

在同一线程中执行await任务之前和之后的代码部分

[英]the part of code before and after await task is executed in same thread

i have the following code in my Console Application Project : 我的控制台应用程序项目中包含以下代码:

static void Main()
{
    Console.WriteLine($@"{Thread.CurrentThread.ManagedThreadId}, main");
    var list = new List<Task<int>>();
    for (int i = 1; i < 4; i++)
    {
        list.Add(GetValueAsync(i, i, i));
        //list.Add(GetValueAsync());
    }

    var whenAll = Task.WhenAll(list);
    whenAll.Wait();

    Console.WriteLine(@"End program");
    Console.ReadLine();
}


private static async Task<int> GetValueAsync(int val, int delay, int taskId)
{
    Console.WriteLine($@"{Thread.CurrentThread.ManagedThreadId}, task{taskId} sleep");

    await Task.Delay(1000 * delay);

    Console.WriteLine($@"{Thread.CurrentThread.ManagedThreadId}, task{taskId} awake");
    return val;
}

private static int index = 0;
private static async Task<int> GetValueAsync()
{
    index++;
    Console.WriteLine($@"{Thread.CurrentThread.ManagedThreadId}, task{index} sleep");

    await Task.Delay(2000);

    Console.WriteLine($@"{Thread.CurrentThread.ManagedThreadId}, task{index} awake");
    return 10;
}

when i use GetValueAsync with parameters i get this one : 当我使用带有参数的 GetValueAsync时我得到了这一点:

在此处输入图片说明

when i use GetValueAsync without parameters i get this one : 当我使用不带参数的 GetValueAsync时我得到了这个:

在此处输入图片说明

Why do the part of code after await Task.Delay(1000 * delay) is executed in the same thread , when i use GetValueAsync(int val, int delay, int taskId) ? 当我使用GetValueAsync(int val, int delay, int taskId)为什么在同一线程中执行await Task.Delay(1000 * delay)之后的代码部分? if i am not wrong , the part after await supposed to be executed in the new thread... (current project is a console application , the main thread is not GUI thread ) 如果我没有记错,那么等待的部分应该在新线程中执行...(当前项目是控制台应用程序,主线程不是GUI线程)

These are always hard questions to answer as the answers never seem satisfying to people new to the topic 这些问题总是很难回答的,因为对于刚接触该主题的人来说,答案似乎从未令人满意

If you change your code to the following. 如果将代码更改为以下内容。 you will get the expected results 您将获得预期的结果

list.Add(GetValueAsync(i, 1, i));

Output 输出量

1, main
1, task1 sleep
1, task2 sleep
1, task3 sleep
5, task2 awake
4, task3 awake
6, task1 awake
End program

So whats wrong with the first results? 那么最初的结果怎么了? Simply, more threads are needed, the callbacks happen sufficiently delayed to reuse the same thread. 简单地说,需要更多线程,回调发生足够的延迟才能重用同一线程。 The Task Scheduler sees no need to allocate any more (or different) resources for you 任务计划程序认为无需为您分配更多(或不同)资源

your 2 functions have not the same delay 你的两个功能有不同的延迟

the first have a variable delay (1, 2 and 3 sec), and the second have constant delay (2 sec) 第一个具有可变延迟(1、2和3秒),第二个具有恒定延迟(2秒)

if you write 如果你写

await Task.Delay(2000); //instead of await Task.Delay(1000 * delay);

you will have the same result than the second function 您将获得与第二个功能相同的结果

The threadid is not really an incremental value, when a thread is finished, its "id" becomes available threadid并不是真正的增量值,当线程完成时,其“ id”可用

The same threadID is a different task in your case 在您的情况下,相同的threadID是不同的任务

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

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