我该如何等待任务开始。下面的代码失败:如何等待任务启动?
var asyncmethod = ...a Task<TReturn>, with .Start() called on it...;
int waitcounter = 0;
while (!asyncmethod.Wait(1000))
{
waitcounter++;
Log("waiting very long...");
}
ret = asyncmethod.Result;
的asyncmethod.Wait(1000)等待1秒如预期,但任务处于状态WaitingToRun并且永远不会开始运行等待()ING时。另一方面,当调用.Result时,它将开始运行。如何让它在没有调用的情况下运行.Result?
'伺机asyncmethod()'? – ieaglle
请向我们展示您的问题的最小完整重现,而不是伪代码。你为什么要创建一个任务并立即用'Wait'阻止它? –
为什么不使用“Thread.Sleep(1000);”代替? – Rariolu