0

On a WPF application I have (too easy to complicate it with any MVVM monstrosity) I start another thread when the main form, is created. The thread is NOT a background thread. I need to be sure that some unmanaged resources I open are correctly cleaned up when the thread closes.

The thread is created and it works under the whole application's life. Now I close the main window (I have ShutdownMode="OnMainWindowClose"). On my Closed event I signalize the thread to terminate and wait for it (Join). When debugging and setting a break point on the Closed logic, it works fine. The thread terminates and the application closes gratefully.

Running the program however it seems like the code in Closed sometimes doesn't get executed? The application closes but I see a zombie process in the TaskManager like if the second thread is still running.

Any ideas?

Ulf Honkanen
  • 119
  • 1
  • 8
  • 2
    Showing relevant parts of your code would help us to help you. As a suggestion - have you tried writing debug info to a file - you should get an idea of the sequence of events. – PaulF Feb 28 '18 at 16:21
  • Have you considered MemoryBarrier [Why we need Thread.MemoryBarrier()?](https://stackoverflow.com/questions/3556351/why-we-need-thread-memorybarrier) – Rekshino Mar 01 '18 at 07:25
  • Put some log output to the handler to ensure it be(or not) invoked. – Rekshino Mar 01 '18 at 07:26
  • `The application closes but I see a zombie process in the TaskManager like if the second thread is still running.` You should attach a debugger to the 'zombie process' to check where it's stuck – Kevin Gosse Mar 01 '18 at 07:32

0 Answers0