C#:异常后停止线程 [英] C#: Stopping a thread after an Exception

查看:271
本文介绍了C#:异常后停止线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码:

Thread t = new Thread(() => UpdateImage(origin));
t.Name = "UpdateImageThread";
t.Start();

如果方法UpdateImage(来源)抛出一个异常,就必须停止线或将被采空异常后?

If method UpdateImage(origin) throw an exception, it is necessary to stop thread or it will be stoped after the exception?

感谢您!

推荐答案

如果 UpdateImage 抛出一个异常,它可能会记下你的整个过程。这又引出一个顶级的异常任何线索表明一个大问题。你应该把尝试 / 围绕 UpdateImage 和做适当的事情。是的,如果异常到达一个线程的顶部,线程是死的:

If UpdateImage throws an exception, it is probably going to take down your whole process. Any thread that raises a top-level exception indicates a big problem. You should wrap this, for example by putting try/catch around UpdateImage and doing something suitable. And yes, if an exception gets to the top of a thread, the thread is dead:

Thread t = new Thread(() => {
    try {UpdateImage(origin); }
    catch (Exception ex) {Trace.WriteLine(ex);}
});
t.Name = "UpdateImageThread";
t.Start();



(或您的错误处理的选择)

(or your choice of error handling)

这篇关于C#:异常后停止线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆