简体   繁体   English

主线程是否会捕获另一个线程引发的异常?

[英]Will main thread catch exception thrown by another thread?

If I have code like this: 如果我有这样的代码:

try
{
    Thread t = new Thread(new ThreadStart(wc.LocalRunProcess));
    t.IsBackground = true;
    t.Start();
}
catch (Exception ex)
{
    //do something with ex
}

Will the exception thrown by thread t be caught in the catch block? 线程t抛出的异常是否会在catch块中被捕获?

No. It will not catch any exceptions in your other thread t . 不,它不会赶上你的其他线程的任何异常t You will have to catch them in that thread and deal with them appropriately. 你必须在那个线程中捕获它们并适当地处理它们。

However, I believe the AppDomain's UnhandedException event will report it. 但是,我相信AppDomain的UnhandedException事件会报告它。

Take a look at System.ComponentModel.BackgroundWorker! 看看System.ComponentModel.BackgroundWorker! It has exception and cancellation handling. 它有例外和取消处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM