Multithreading WinRT在非UI线程中运行时不报告异常
在一个线程中,我捕获ThreadCanceledException,而不是其他线程,希望看到错误(如果有的话)。但是,当线程启动并且代码有错误时,它会生成消息Multithreading WinRT在非UI线程中运行时不报告异常,multithreading,windows-runtime,Multithreading,Windows Runtime,在一个线程中,我捕获ThreadCanceledException,而不是其他线程,希望看到错误(如果有的话)。但是,当线程启动并且代码有错误时,它会生成消息 A first chance exception of type 'System.Exception' occurred in ... The thread ... has exited with code 259 (0x103) 并优雅地退出,而不显示实际的例外情况。只有在UI线程中运行代码时才能识别错误 有没有办法在线程中报告实际异
A first chance exception of type 'System.Exception' occurred in ...
The thread ... has exited with code 259 (0x103)
并优雅地退出,而不显示实际的例外情况。只有在UI线程中运行代码时才能识别错误
有没有办法在线程中报告实际异常?您可以使用事件跟踪这些异常: 请注意,此事件不会立即触发,而是在垃圾收集器收集错误任务之后触发。这是因为创建任务的代码应该是观察异常的代码(例如,通过调用
task.Wait()
或task.Result
,或通过等待它)。只有当运行时确定没有其他人可以观察到异常时,才会触发该事件
TaskScheduler.UnobservedTaskException += (sender, eventArgs) =>
{
Debug.WriteLine("Unobserved exception: {0}", eventArgs.Exception);
};