Application doesn't exit with 0 threads
        Posted  
        
            by Bryce Wagner
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Bryce Wagner
        
        
        
        Published on 2010-04-23T15:38:55Z
        Indexed on 
            2010/04/23
            15:43 UTC
        
        
        Read the original article
        Hit count: 475
        
We have a WinForms desktop application, which is heavily multithreaded. 3 threads run with Application.Run and a bunch of other background worker threads. Getting all the threads to shut down properly was kind of tricky, but I thought I finally got it right.
But when we actually deployed the application, users started experiencing the application not exiting. There's a System.Threading.Mutex to prevent them from running the app multiple times, so they have to go into task manager and kill the old one before they can run it again.
Every thread gets a Thread.Join before the main thread exits, and I added logging to each thread I spawn. According to the log, every single thread that starts also exits, and the main thread also exits. Even stranger, running SysInternals ProcessExplorer show all the threads disappear when the application exits. As in, there are 0 threads (managed or unmanaged), but the process is still running.
I can't reproduce this on any developers computers or our test environment, and so far I've only seen it happen on Windows XP (not Vista or Windows 7 or any Windows Server). How can a process keep running with 0 threads?
© Stack Overflow or respective owner