Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Stopping a thread after an Exception

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?

Thank you!

like image 415
VansFannel Avatar asked Jul 15 '09 08:07

VansFannel


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

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)

like image 63
Marc Gravell Avatar answered Oct 11 '22 18:10

Marc Gravell


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.

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.

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.

like image 32
Fabian Schmied Avatar answered Oct 11 '22 18:10

Fabian Schmied