我有两个异步方法,我在表单窗口的背景下作为单独的线程/任务运行。这些是无限循环,只是在后台执行一些工作,然后使用调度程序更新UI。见下文。Task.WhenAll没有按预期抛出异常
public async Task RunCameraThread(CancellationToken cancelToken)
{
while (true)
{
// If cancellation token is set, get out of the thread & throw a cancel exception
cancelToken.ThrowIfCancellationRequested();
// Get an image from the camera
CameraBitmap = Camera.CaptureImage(true);
// Update the UI (use lock to prevent simultaneous use of Dispatcher object in other thread)
lock (Dispatcher)
{
Dispatcher.Invoke(() => pictureBoxCamera.Image = tempBitmap);
Dispatcher.Invoke(() => pictureBoxCamera.Invalidate());
}
}
}
public async Task RunDistanceSensorThread(CancellationToken cancelToken)
{
while (true)
{
// If cancellation token is set, get out of the thread & throw a cancel exception
cancelToken.ThrowIfCancellationRequested();
// Get the distance value from the distance sensor
float distance = Arduino.AverageDistance(10, 100);
// Update the UI (use lock to prevent simultaneous use of Dispatcher object)
lock (Dispatcher)
{
Dispatcher.Invoke(() => textBoxDistanceSensor.Text = distance.ToString("0.00"));
}
}
}
这些任务是通过单击按钮(代码如下)启动的。我试图使用等待Task.WhenAll为了等待这两个任务。当设置取消标记时,它将按预期工作,并捕获OperationCanceledException。但是,由Camera或Arduino问题引发的任何异常(在运行期间通过拔下USB来模拟)似乎都没有被捕获。
private async void buttonConnect_Click(object sender, EventArgs e)
{
try
{
// Disable UI so we cannot click other buttons
DisableComponentsUI();
// Connect to Nimbus, Camera and Arduino
await Task.Run(() => Nimbus.ConnectAsync());
Camera.Connect();
Camera.ManagedCam.StartCapture();
Arduino.Connect();
// Get the current Nimbus positions and enable UI
UpdatePositionsUI();
EnableComponentsUI();
// Reset cancel token and start the background threads and await on them (this allows exceptions to bubble up to this try/catch statement)
StopTokenSource = new CancellationTokenSource();
var task1 = Task.Run(() => RunCameraThread(StopTokenSource.Token));
var task2 = Task.Run(() => RunDistanceSensorThread(StopTokenSource.Token));
await Task.WhenAll(task1, task2);
}
catch (OperationCanceledException exceptionMsg)
{
// Nothing needed here...
}
catch (Hamilton.Components.TransportLayer.ObjectInterfaceCommunication.ComLinkException exceptionMsg)
{
NimbusExceptionHandler(exceptionMsg);
}
catch (FlyCapture2Managed.FC2Exception exceptionMsg)
{
CameraExceptionHandler(exceptionMsg);
}
catch (IOException exceptionMsg)
{
ArduinoExceptionHandler(exceptionMsg);
}
catch (UnauthorizedAccessException exceptionMsg)
{
ArduinoExceptionHandler(exceptionMsg);
}
catch (TimeoutException exceptionMsg)
{
ArduinoExceptionHandler(exceptionMsg);
}
}
奇怪的是,我看到在输出窗口中抛出异常,但他们不会冒泡到我的try/catch。另外,如果我只是在等待一项任务,它按预期工作,异常冒泡。
任何人有任何想法我做错了什么?
谢谢!
@StenPetrov是的,我期待着。它似乎会抛出一个异常,因为我可以在输出窗口中看到它,但是它在使用时不会冒泡到我的try/catch中。WhenAll – leonhart88
@StenPetrov'WhenAll'会抛出一个包含异常形式的'AggregateException',形成每个发生故障的任务的每一个异常,而不仅仅是第一个异常。 – Servy
@servy你确定吗?我想你必须在'WhenAll'任务上调用'Wait',如果你想捕获单个任务抛出的异常,如[这里]所示(https://msdn.microsoft.com/en-us/library/hh194874( v = vs.110)的.aspx)。但是,我不知道与“等待WhenAll”任务的关系。 – Quantic