gpt4 book ai didi

c# - 当一个任务引发另一个任务与第一个任务同步时,Task.WaitAll挂起

转载 作者:行者123 更新时间:2023-12-03 13:08:37 27 4
gpt4 key购买 nike

Task.WaitAll(t1, t2)的执行与t2同步但t1同步到t1的点之前引发异常时,应该从t2中得到什么?在这种情况下,很明显t2将永远不会完成。但是因为t1引发异常,所以我希望Task.WaitAll(t1,t2)会返回聚合异常,指示任务之一失败。

然而,这种情况并非如此。原来Task.WaitAll一直挂着等待。

我可以为这种行为辩解说,Task.WaitAll做了它声称要等待所有任务返回的操作,即使其中一个已经抛出异常也是如此。尽管我不喜欢它,但对我来说仍然可以,只要我知道就可以了。

但是,我的问题是,是否有Task.WaitAll的替代API,并且的行为是“等待所有任务完成,除非任务中的一个抛出异常” ?我想这是我大多数时候需要的行为。

编辑1

我最初使用TaskCompletionSource<>进行同步。但这对我想提出的观点无关紧要。因此,我通过基本投票对其进行了更改。

编辑2

我写了一个F#等效程序(见下文),发现它实际上确实具有我所期望的行为。如上所述,Task.WhenAllTask.WaitAll等待所有任务完成,即使其中一部分失败了。但是,像the document所述并在以下程序中测试的,当任何子任务失败时,Async.Parallel(与F#中的WhenAll等效)会急于失败。

If all child computations succeed, an array of results is passed to the success continuation. If any child computation raises an exception, then the overall computation will trigger an exception, and cancel the others. The overall computation will respond to cancellation while executing the child computations. If cancelled, the computation will cancel any remaining child computations but will still wait for the other child computations to complete.



C#是否与F#的 Async.Parallel等效,即在等待所有任务完成时,只要子任务失败,它就会保释并抛出?

我使用的示例:
public void Test()
{
bool t1done = false;

Task t1 = Task.Run(() =>
{
DoThing(); // throw here
t1done = true;
});

Task t2 = Task.Run(async () =>
{
// polling if t1 is done.
// or equivelantly: SpinWait.SpinUntil(() => t1done);
while (!t1done) await Task.Delay(10);
});

// t2 will never finish, but t1 threw. So I expect Task.WaitAll to throw rather than blindly wait
// for t2 EVEN THOUGH t1 threw already.
Task.WaitAll(t1, t2); // never returns
// or this could be an async wait, but still this function would never return:
// await Task.WhenAll(t1,t2);

Console.WriteLine("done");
}

void DoThing()
{
throw new InvalidOperationException("error");
}

F#“等效”具有我期望的行为:
[<EntryPoint>]
let main argv =
let mutable ready : bool = false
let task1 = async {
failwith "foo"
ready <- true
}

let task2 = async {
while not ready do do! Async.Sleep 100
}

[| task1; task2 |]
|> Async.Parallel // Equivelant to Task.WhenAll() - combining task1 and task1 into a single Async,
// but it throws instead of waiting infinately.
|> Async.RunSynchronously // run task
|> ignore

0

最佳答案

我知道没有等效的API。

我建议您确保任务t2可以完成!然后,WaitAll()将按您期望的那样从t1引发异常,它使您可以更好地控制任务t2发生的情况。

假设您的t2任务在开始轮询t1以完成操作之前要完成全部其他工作,则可以从并行运行的两个任务中受益。您如何知道在t1引发异常之前完成了多少工作?

WaitAll并不关心任务如何完成(已取消,有故障或RanToCompletion),因为它只是其中之一。

当您可以检查Task本身的结果时,实际上不需要变量t1done:

Task t1 = Task.Run(() =>
{
throw null;
});

Task t2 = Task.Run(async () =>
{
// check whether task 1 is finished yet
while (!t1.IsCompleted) await Task.Delay(10);
if (t1.Status == TaskStatus.RanToCompletion)
{
// we know that t1 finished successfully and did not throw any
// error.
}
else
{
Console.WriteLine("We know that t1 did not run to completion");
}
});

try
{
Task.WaitAll(t1, t2);
// this will now throw the exception from t1 because t2 can also finish
}
catch (AggregateException)
{

}

// For interest sake, you can now view the status of each task.
Console.WriteLine(t1.Status);
Console.WriteLine(t2.Status);
Console.WriteLine("Finished");

现在,我不能说这是否是最适合您解决方案的设计。您是否研究了任务继续?因为如果t2除了等待t1完成以外什么都不做,那么还有更好的选择……但是这应该可以直接回答您所提出的问题。

关于c# - 当一个任务引发另一个任务与第一个任务同步时,Task.WaitAll挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47820918/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com