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;
...
- Will the second thread wait for method Execute to finish executing?
- 第二个线程会等待Execute方法完成执行吗?
- 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;
CurrentIteration = Execute(); //返回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!!!!!
. Why is that? How can I achieve that? Thanks!
但从来没有线程2结束!!!!!这是为什么?我怎样才能做到这一点?谢谢!
1 个解决方案
#1
4
You can await
on a task from multiple threads. You were actually really close to get that to work, as @Rob said, you just needed to await
the second thread.
您可以从多个线程等待任务。你真的很接近让它工作,正如@Rob所说,你只需要等待第二个线程。
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. If you would've pressed Enter, you can see the "thread 2 ended!!!!!" line for a second before the app closes.
您没有看到该指示的原因是因为控制台卡在ReadKey方法上,并且在完成之前无法写入任何内容。如果你按Enter键,你可以看到“线程2结束!!!!!”应用程序关闭前的第二行。
#1
4
You can await
on a task from multiple threads. You were actually really close to get that to work, as @Rob said, you just needed to await
the second thread.
您可以从多个线程等待任务。你真的很接近让它工作,正如@Rob所说,你只需要等待第二个线程。
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. If you would've pressed Enter, you can see the "thread 2 ended!!!!!" line for a second before the app closes.
您没有看到该指示的原因是因为控制台卡在ReadKey方法上,并且在完成之前无法写入任何内容。如果你按Enter键,你可以看到“线程2结束!!!!!”应用程序关闭前的第二行。