Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-Threading - Cleanup strategy at program end

What is the best way to finish a multi-threaded application in a clean way?
I am starting several socket connections from the main thread in seperate sockets and wait until the end of my business day in the main thread and use currently System.Environment.Exit(0) to terminate it.

This leads to an unhandled exception in one of the childs. Should I stop the threads from the list? I have been reluctant to implement any real stopping in the childs yet, thus I am wondering about the best practice. The sockets are all wrapped nicely with proper destructors for logging out and closing, but it still leads to errors.

like image 365
weismat Avatar asked Mar 12 '10 14:03

weismat


1 Answers

have a look at jon skeet's articles about multithreading:

http://www.yoda.arachsys.com/csharp/threads/

especially "Shutting down worker threads gracefully":

http://www.yoda.arachsys.com/csharp/threads/shutdown.shtml

like image 122
stmax Avatar answered Oct 06 '22 00:10

stmax