Stop Thread and exit program  
Author Message
danni123





PostPosted: Visual C# General, Stop Thread and exit program Top

My program has a form and another worker class.

Worker class has a thread that receiving data, in loop: socket.ReceiveFrom(....)
When I close the form the program (thread) is still running.

How to stop everything and exit





Visual C#5  
 
 
Paul Louth





PostPosted: Visual C# General, Stop Thread and exit program Top

How are you starting the thread
 
 
danni123





PostPosted: Visual C# General, Stop Thread and exit program Top

I start in the worker class' constructor: (a create a new instance of Listen class in my form)

class Listen
{
public Thread srvThread;

public Listen()
{

try
{
//Starting the UDP Server thread.
srvThread = new Thread(new ThreadStart(StartReceive));
ssrvThread.Start();


.
.
.
}

 
 
Paul Louth





PostPosted: Visual C# General, Stop Thread and exit program Top

So in your form's Dispose method or OnClosing event you should call a stop method on your Listen class, which in turn calls srvThread.Stop()


 
 
danni123





PostPosted: Visual C# General, Stop Thread and exit program Top

But there is No Stop() method for thread!

It has an Abort(), but it's not working here...

 
 
Paul Louth





PostPosted: Visual C# General, Stop Thread and exit program Top

Sorry, I meant Abort.  Why isn't it working   Are you getting an error
 
 
danni123





PostPosted: Visual C# General, Stop Thread and exit program Top

no errors, but the thread still runing

 
 
danni123





PostPosted: Visual C# General, Stop Thread and exit program Top

I think it happens because of :
srv.ReceiveFrom(....) is a blocking method....

 
 
Peter Ritchie





PostPosted: Visual C# General, Stop Thread and exit program Top

For Abort() to work your thread method *has* to process the ThreadAbortException. If the thread is blocked on another call it can't process that exception.

If you simply want your thread to terminate when you close your application you can simply set the threads IsBackground to true before calling Start(). The CLR will not let an application terminate if non-background threads are still running.

Ideally you should implement a way to deterministically stop your threads.

 
 
OmegaMan





PostPosted: Visual C# General, Stop Thread and exit program Top

This may be off base...but you may want to create a custom ApplicationContext for your program to run within....

When the form(s) close or are being closed, one can capture that event and handle the worker threads from an outside context. If this advice does not sound off the mark, check out the multi-form example shown in the ApplicationContext example in MSDN Application.Run Method (ApplicationContext)


 
 
Peter Ritchie





PostPosted: Visual C# General, Stop Thread and exit program Top

 
This may be off base...but you may want to create a custom ApplicationContext for your program to run within....

When the form(s) close or are being closed, one can capture that event and handle the worker threads from an outside context. If this advice does not sound off the mark, check out the multi-form example shown in the ApplicationContext example in MSDN Application.Run Method (ApplicationContext) 
Handling the form close/closing events in an ApplicationContext won't help much unless references to the thread objects are available in that scope.  If the thread's can't handle being aborted or terminated, it won't do much good anyway.  Sounds like the thread objects are created as members of the Form class; in which case handling the form's closing/closed events would be easier.  Using an ApplicationContext would certainly be the way to go if more than one instance of a form was used though and thread objects where shared amongst them...

 
 
OmegaMan





PostPosted: Visual C# General, Stop Thread and exit program Top

Handling the form close/closi ng events in an ApplicationContext won't help much unless references to the thread objects are available in that scope.


I concur with that wholeheartedly...I am just playing color man to your commentary and throwing out suggestions. <g>