views:

437

answers:

6

Hi there,

I've "inherited" a legacy C#/C++ program that I have to debug. The current problem is that the .exe won't stop after I close the program, i.e. it still shows up in Task Manager.

This is a problem, because it won't let me restart the program, because only one instance can run. Often killing the process doesn't work; I'm forced to reboot.

I was under the impression that when the main program stopped, all the child threads were also supposed to stop, but I may be wrong.

Q: What would cause a .exe to not stop?

+9  A: 

Child threads will not stop automatically unless they have been specifically set as background threads (i.e., with thread.IsBackground = true).

Edit: It is also possible that the main thread isn't terminating when the form is closed (i.e., there's other code that is set to run after close that isn't completing).

Eric Rosenberger
+2  A: 

My guess would be that all threads aren't stopped. Usually you can end task the program and it will stop though. Maybe one thread has hung on to a system resource that's more difficult to release.

If you can get a debug build, run the program and after the program is "exited", hit the pause button in the debug window. At that point you can look through the threads and find out which one is hung. To help, you should name your threads when they get created (it's an extra parameter in the .Start function)

FryGuy
A: 

At the most frundamental level, there is an infinite loop somewhere.

BubbaT
Not necessarily - it could be a deadlock or livelock.
LeopardSkinPillBoxHat
What's a livelock? :-O
M. Jahedbozorgan
+2  A: 

You may want to look into process explorer. It makes it easier to shut down the programs and it can view the threads of the program and potentially point you in the right direction of where to make the the program behave.

Jeff Martin
+3  A: 

I find it useful to attach to the running process with the debugger and press the pause button. After that I would inspect the Threads window and see what the stack trace is doing for each of the executing threads. The threads window is hidden by default. Here is more information about how to show it and use it:

http://msdn.microsoft.com/en-us/library/w15yf86f.aspx

Steven Behnke
+1 for stating a doable approach
Cecil Has a Name
A: 

Is the startup form the one that's being closed to exit the application? If it isn't you need to put Application.Exit() or Environment.Exit() in the form.closed event of the form that is closing last.