我目前正在尝试写一个可靠的超时值的状态检查工具。我看到如何做到这一点的方法之一是使用Task.WhenAny()和包括Task.Delay,但它似乎并没有产生我所期望的结果:并行执行一组任务,但有一个组超时
public void DoIUnderstandTasksTest()
{
var checkTasks = new List<Task>();
// Create a list of dummy tasks that should just delay or "wait"
// for some multiple of the timeout
for (int i = 0; i < 10; i++)
{
checkTasks.Add(Task.Delay(_timeoutMilliseconds/2));
}
// Wrap the group of tasks in a task that will wait till they all finish
var allChecks = Task.WhenAll(checkTasks);
// I think WhenAny is supposed to return the first task that completes
bool didntTimeOut = Task.WhenAny(allChecks, Task.Delay(_timeoutMilliseconds)) == allChecks;
Assert.True(didntTimeOut);
}
缺少什么我在这里?
它会产生什么结果? “不起作用”是一个非常普遍的术语,很难让我们知道什么是错的。例外?编译错误?从来没有从第二次WhenAll返回? –