Quit program cleanly

  • Thread starter Thread starter Xarky
  • Start date Start date
X

Xarky

Hi,
I have a program, that makes use of threads. Now when I quit the
program from the upper right button of the window, the threads are not
terminated and the program is still is running.

How can I handle the event for that button.



Thanks in Advance
 
Simple. Code what your thread stopping handler into the "closing"
event of the form.

got a code problem? hand it to the devil.
 
Call Thread.Abort, then Thread.Join.
This causes a ThreadAbortException to be raised in the thread, and this is
an exception that 'rethrows' itself. Although I'm not sure of the exact
semantics of this, what I do know IIRC though is that
a) you need to handle any thread cleanup code in the 'finally' of the
exception handler that can catch a threadabortexception.
b) when the calling thread has called 'Join', and that call returns, it can
be confident the thread is dead.
 
Do *not* call Thread.Abort. See

http://www.interact-sw.co.uk/iangblog/2004/11/12/cancellation

and my reply

http://www.dotnetconsult.co.uk/weblog/PermaLink.aspx/4f52c396-1b0d-4419-8871-6ca6992460ca

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Call Thread.Abort, then Thread.Join.
This causes a ThreadAbortException to be raised in the thread, and this is
an exception that 'rethrows' itself. Although I'm not sure of the exact
semantics of this, what I do know IIRC though is that
a) you need to handle any thread cleanup code in the 'finally' of the
exception handler that can catch a threadabortexception.
b) when the calling thread has called 'Join', and that call returns, it can
be confident the thread is dead.
 
Hi,

You have to declare the thread as background

From MSDN:
A managed thread is either a background thread or a foreground thread.
Background threads are identical to foreground threads with one exception: a
background thread will not keep the managed execution environment alive.
Once all foreground threads have been stopped in a managed process (where
the .exe file is a managed assembly), the system stops all background
threads and shuts down. A thread can be designated as a background or a
foreground thread by setting the Thread.IsBackground property. For example,
a thread can be designated a background thread by setting
Thread.IsBackground to true. A thread can likewise be designated a
foreground thread by setting IsBackground to false. All threads that enter
the managed execution environment from unmanaged code are marked as
background threads. All threads generated by creating and starting a new
Thread object are foreground threads. If you create a foreground thread that
you want to listen for some activity, such as a socket connection, you
should set Thread.IsBackground to true, so that your process can terminate.


Cheers,
 
Back
Top