Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading WinRT在非UI线程中运行时不报告异常_Multithreading_Windows Runtime - Fatal编程技术网

Multithreading WinRT在非UI线程中运行时不报告异常

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线程中运行代码时才能识别错误 有没有办法在线程中报告实际异

在一个线程中,我捕获ThreadCanceledException,而不是其他线程,希望看到错误(如果有的话)。但是,当线程启动并且代码有错误时,它会生成消息

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);
};