在 iprocess 完成之前等待继续
await continues before iprocess finishes
我创建了一个运行 X 个线程的自定义工厂。我的问题是我希望我的 ContinueWiths 在等待之后执行代码之前完成。
调用代码:
Task t = _manager.RunCalcsCustomFactory(int.Parse(textTasksToCreate.Text), int.Parse(textMaxThreads.Text), cancelSource.Token, new Progress<string>(UpdateStatus), new Progress<int>(UpdateProgress));
Task errorHandlingTask = t.ContinueWith((tt) => ProcessError(), TaskContinuationOptions.OnlyOnFaulted);
Task cancelTask = t.ContinueWith((tttt) => TasksComplete(), TaskContinuationOptions.OnlyOnCanceled);
await Task.WhenAll(t);
UpdateStatus("Done");
在我的任务中,我立即抛出一个错误。所以我希望我的 ProcessError 在执行 UpdateStatus("Done")
之前完成。
这是我的输出:
start thread10
Waiting for tasks to finish.
Exception: Too many connections open.
Done
Error: Too many connections open.
ERROR
注意如果在 RunCalcsCustomFactory 中 UpdateStatus("Error: Too many connections open.")
之前显示完成并且 UpdateStatus("ERROR")
在 ProcessError 中是 运行。
我想要 UpdateStatus("Done")
运行 从 RunCalcsCustomFactory 回调完成并且 ProcessError 完成后。
任何时候你的代码中有 ContinueWith
,你应该只使用 async
/await
来代替:
try
{
await _manager.RunCalcsCustomFactory(int.Parse(textTasksToCreate.Text), int.Parse(textMaxThreads.Text), cancelSource.Token, new Progress<string>(UpdateStatus), new Progress<int>(UpdateProgress));
}
catch (OperationCanceledException)
{
TasksComplete();
}
catch (Exception ex)
{
ProcessError();
}
UpdateStatus("Done");
我创建了一个运行 X 个线程的自定义工厂。我的问题是我希望我的 ContinueWiths 在等待之后执行代码之前完成。
调用代码:
Task t = _manager.RunCalcsCustomFactory(int.Parse(textTasksToCreate.Text), int.Parse(textMaxThreads.Text), cancelSource.Token, new Progress<string>(UpdateStatus), new Progress<int>(UpdateProgress));
Task errorHandlingTask = t.ContinueWith((tt) => ProcessError(), TaskContinuationOptions.OnlyOnFaulted);
Task cancelTask = t.ContinueWith((tttt) => TasksComplete(), TaskContinuationOptions.OnlyOnCanceled);
await Task.WhenAll(t);
UpdateStatus("Done");
在我的任务中,我立即抛出一个错误。所以我希望我的 ProcessError 在执行 UpdateStatus("Done")
之前完成。
这是我的输出:
start thread10
Waiting for tasks to finish.
Exception: Too many connections open.
Done
Error: Too many connections open.
ERROR
注意如果在 RunCalcsCustomFactory 中 UpdateStatus("Error: Too many connections open.")
之前显示完成并且 UpdateStatus("ERROR")
在 ProcessError 中是 运行。
我想要 UpdateStatus("Done")
运行 从 RunCalcsCustomFactory 回调完成并且 ProcessError 完成后。
任何时候你的代码中有 ContinueWith
,你应该只使用 async
/await
来代替:
try
{
await _manager.RunCalcsCustomFactory(int.Parse(textTasksToCreate.Text), int.Parse(textMaxThreads.Text), cancelSource.Token, new Progress<string>(UpdateStatus), new Progress<int>(UpdateProgress));
}
catch (OperationCanceledException)
{
TasksComplete();
}
catch (Exception ex)
{
ProcessError();
}
UpdateStatus("Done");