假设我有一些异步任务,有时可能运行快,有时可能运行慢,
在上述情况下,Task.ContinueWith() 中的回调函数是否有保障会被执行?
public Random seed = new Random();
private async Task<string> _Work()
{
int time = seed.Next(0, 5000);
string result = string.Format("Worked for {0} milliseconds", time);
await Task.Delay(time);
return result;
}
public void SomeMethod()
{
_Work(); // starts immediately? Am I right?
// since _Work() will be executed immediately before ContinueWith() is executed,
// will there be a chance that callback will not be called if _Work completes very quickly,
// like before ContinueWith() can be scheduled?
_Work().ContinueWith(callback)
}
在上述情况下,Task.ContinueWith() 中的回调函数是否有保障会被执行?
_Work
很快完成,callback
不会被调用? - Kenneth K.