简体   繁体   English

如何在两个不同的线程上等待一项任务?

[英]How to await for one task on two different threads?

Can I await on a Task that was created on a different thread? 我可以等待在其他线程上创建的任务吗? For example: 例如:

...
CurrentIteration = Execute(); // returns Task
await CurrentIteration;
...

And then, on another thread: 然后,在另一个线程上:

...
await CurrentIteration;
...
  1. Will the second thread wait for method Execute to finish executing? 第二个线程会等待方法Execute完成执行吗?
  2. If it will, will I be able to re-use CurrentIteration for the same purpose in the second thread, given that I re-run 如果可以的话,鉴于我重新运行,我是否能够在第二个线程中出于相同的目的重用CurrentIteration

CurrentIteration = Execute(); // returns Task await CurrentIteration;

On the first thread? 在第一个线程上?

I tried this code: 我尝试了这段代码:

public class Program
{
    public static void Main(string[] args)
    {
        MainAsync(args).GetAwaiter().GetResult();
    }
    public static async Task MainAsync(string[] args)
    {

        var instance = new SomeClass();
        var task = instance.Execute();
        Console.WriteLine("thread 1 waiting...");
        Task.Run(async () =>
        {
            Console.WriteLine("thread 2 started... waiting...");
            await task;
            Console.WriteLine("thread 2 ended!!!!!");
        });

        await task;

        Console.WriteLine("thread 1 done!!");

        Console.ReadKey();
    }
}


public class SomeClass
{
    public async Task Execute()
    {
        await Task.Delay(4000);
    }
}

But it prints 但它打印

thread 1 waiting...
thread 2 started... waiting...

then 然后

thread 1 done!!

but never thread 2 ended!!!!! 但是thread 2 ended!!!!!永远不会thread 2 ended!!!!! . Why is that? 这是为什么? How can I achieve that? 我该如何实现? Thanks! 谢谢!

You can await on a task from multiple threads. 您可以await来自多个线程的任务。 You were actually really close to get that to work, as @Rob said, you just needed to await the second thread. 正如@Rob所说,实际上您真的很接近要使它起作用,您只需要await第二个线程即可。

consider this: 考虑一下:

    public static async Task MainAsync(string[] args)
    {

        var instance = new SomeClass();
        var task = instance.Execute();
        Console.WriteLine("thread 1 waiting...");
        var secondTask = Task.Run(async () =>
        {
            Console.WriteLine("thread 2 started... waiting...");
            await task;
            Console.WriteLine("thread 2 ended!!!!!");
        });

        await task;

        await secondTask;

        Console.WriteLine("thread 1 done!!");

        Console.ReadKey();
    }

Add the wait on your second thread after you finish waiting for the task. 等待任务完成后,在第二个线程上添加等待。

The reason you didn't see the indication is because the console got stuck on the ReadKey method, and couldn't write anything until it's finished. 您没有看到指示的原因是因为控制台卡在了ReadKey方法上,并且在完成之前无法编写任何内容。 If you would've pressed Enter, you can see the "thread 2 ended!!!!!" 如果您按Enter键,则会看到“线程2已结束!!!!!!” line for a second before the app closes. 在应用程序关闭之前等待一秒钟。

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

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