Application does not exit with 0 threads

We have a WinForms desktop application that is highly multithreaded. 3 threads with Application.Run and many other threads of the working background. It looks like all threads were closed correctly, but I thought I finally figured it out.

But when we actually deployed the application, users started to experience the application not quitting. There's System.Threading.Mutex to prevent them from starting the application multiple times, so they have to go into the task manager and kill the old one before they can start it again.

Each thread receives a Thread.Join before the main thread exits, and I have added an entry to every thread I create. According to the log, every single thread that starts up also exits, and the main thread exits as well. Even a stranger running the SysInternals ProcessExplorer shows that the threads disappear when the application exits. As in there are 0 threads (managed or unmanaged), but the process is still running.

I cannot reproduce this on any developer computers or in our test environment, and so far I've only seen this on Windows XP (not Vista or Windows 7 or any Windows Server). How can a process work with 0 threads?

Edit:

Here's a little more detail. One of the event loops hosts a Win32 DLL that uses a COM object to talk to a device driver. I put it in my thread because the device driver is time sensitive, and whenever the UI thread blocks for a significant amount of time (for example, before waiting for a database call to complete) it will interfere with the device driver.

So, I changed the code so the main thread will do Thread.Join with the device driver thread. This actually caused the application to block ... it logs a few more calls to the UI thread after the connection ends, and then everything stops. If the device is turned off, the driver never starts and the problem goes away. So it looks like the driver should be responsible for keeping the application alive, even after it has supposedly been disabled.

+2


a source to share


3 answers


We never figured out the root software cause, but it was a special version of the driver that caused this issue and updating to the newer driver fixed the problem.



Unfortunately, this is all I can give in case anyone else runs into a similar problem ...

0


a source


When you create your streams, set IsBackground = true for them. When your main thread / ui app is closed, all created threads will be automatically disconnected.



http://msdn.microsoft.com/en-us/library/system.threading.thread.isbackground.aspx

+1


a source


Is it possible that the children of your Application.Run calls are not ending? Also, what actually causes the application to exit - does it automatically close when all threads have finished (automatically means you wrote some code to do this) or is it simulated by the user?

I had an issue where a racial status happened in my "chain of events" which sometimes resulted in what you see. The last two threads will terminate at the same time, fire the event at the same time and each event decides that it is not the last thread, so the application will continue to run even though the number of threads is zero. To fix this I was able to find and fix the race condition, but you can also use a timer that checks every second or two, gets the number of threads, and if none are open, it kills the application.

0


a source







All Articles