how do you make sure process ends when you close program

razz

Well-known member
Joined
Oct 11, 2008
Messages
67
Programming Experience
Beginner
I wrote a program using VB 2008 Express Edition and noticed that when I close the program (by clicking the "X" top right corner), sometimes the program is still showing in processes within Task Manager.

What do I need to include in my programming to make sure this does not occur - i.e. make sure the process ends when I exit the program?

Thank you in advance for your time.
 
This happens if you have started a new foreground thread that is still running when main UI thread ends. You'd have to tell those threads to stop. You can also make secondary threads work as background threads and they will end when application ends, but may still need to synchronize to stop and cleanup other threads. Foreground and Background Threads
 
There is no method you can call that will stop all foreground threads, if you have started another foreground thread it is also your reponsibility to stop it when user request it. You can do this by calling the Abort method of the thread instance, which is not recommended unless you know it does no harm. Another option is to set a variable of some sorts that the thread checks periodically and stops working asap when told to. Sometimes this is not possible due to blocking calls and the only option is to Abort, but if this is the case you should be prepared to Catch the ThreadAbortException and do what is necessary to clean up possible loose ends.

Running threads in background with no thought for what happens when app closes solves most novice problems in this regard ;)
 
Back
Top