C#: Stopping a thread after an Exception

c++exception-handlingmultithreading

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!

Best Solution

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)