C#:在异常后停止线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1130091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:53:49  来源:igfitidea点击:

C#: Stopping a thread after an Exception

c#exception-handlingmultithreading

提问by VansFannel

I have this code:

我有这个代码:

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

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

如果方法 UpdateImage(origin) 抛出异常,需要停止线程还是异常后会停止?

Thank you!

谢谢!

采纳答案by Marc Gravell

If UpdateImagethrows 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/catcharound UpdateImageand doing something suitable. And yes, if an exception gets to the top of a thread, the thread is dead:

如果UpdateImage抛出异常,它可能会破坏你的整个过程。任何引发顶级异常的线程都表示存在大问题。你应该把这个包起来,例如try/catch左右UpdateImage,做合适的事情。是的,如果异常到达线程的顶部,则该线程已死:

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

(or your choice of error handling)

(或您选择的错误处理)

回答by Matt Howells

The thread will terminate automatically as you are not handling the exception, along with the rest of your process, assuming you are on .Net 2.0 or greater (which I assume you are due to the C# 3 syntax in the question).

假设您使用的是 .Net 2.0 或更高版本(我假设您是由于问题中的 C# 3 语法),线程将自动终止,因为您没有处理异常以及您的进程的其余部分。

回答by Marc Gravell

The exception will not cause the thread to stop if it is caught somewhere in the UpdateImage method - unless the catch clause explicitly returns from the method.

如果在 UpdateImage 方法中的某处捕获异常,则该异常不会导致线程停止 - 除非 catch 子句从该方法显式返回。

If it is unhandeled, your application will crash anyway - thus causing the Thread to stop ;)

如果未处理,您的应用程序无论如何都会崩溃 - 从而导致线程停止;)

It is best to place a try...catch block in your UpdateImage method and perform your logic error handling there where it belongs. You can then decide for yourself weather to return and end the thread or try again

最好在 UpdateImage 方法中放置一个 try...catch 块,并在它所属的地方执行逻辑错误处理。然后你可以自己决定天气返回并结束线程或再试一次

回答by Ahmed Said

it is like the main thread, for example if there is exception occured in the main thread and you no one catch it, so the main thread'll terminate and your application.

它就像主线程,例如,如果主线程中发生异常并且您没有人捕获它,那么主线程将终止并且您的应用程序将终止。

The same thing for user threads

用户线程也一样

回答by Fabian Schmied

Since .NET 2.0, when a background thread throws an exception (that is not handled), the .NET runtime will take down your process. In a Windows.Forms application, this is different; you can use the Application.ThreadException event to catch the exception.

从 .NET 2.0 开始,当后台线程抛出异常(未处理)时,.NET 运行时将关闭您的进程。在 Windows.Forms 应用程序中,这是不同的;您可以使用 Application.ThreadException 事件来捕获异常。

This was different in .NET 1.0/1.1, you can read about the whole topic here (e.g. how to enable the legacy behavior with .NET 2.0 or later): http://msdn.microsoft.com/en-us/library/ms228965.aspx#ChangeFromPreviousVersions.

这在 .NET 1.0/1.1 中有所不同,您可以在此处阅读整个主题(例如,如何使用 .NET 2.0 或更高版本启用旧行为):http: //msdn.microsoft.com/en-us/library/ ms228965.aspx#ChangeFromPreviousVersions

No matter whether you use Windows.Forms or the legacy behavior - if the process does not exit, you do not need to explicitly stop the thread; the exception will stop it.

无论你使用 Windows.Forms 还是遗留行为——如果进程没有退出,你不需要显式停止线程;异常将阻止它。