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.
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)
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...
Stop Thread and exit program
Mike9000
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()
Mitch Walker - MSFT
I concur with that wholeheartedly...I am just playing color man to your commentary and throwing out suggestions. <g>
Simon Dahlbacka
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.
xplosiv_1
Jorne
It has an Abort(), but it's not working here...
GuyFawkes
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)
Daikoku
KimberlyL
Maddinel
AdrianR
class Listen
{
public Thread srvThread;
public Listen()
{
try
{
//Starting the UDP Server thread.
srvThread = new Thread(new ThreadStart(StartReceive));
ssrvThread.Start();
.
.
.
}
anu_ooo
srv.ReceiveFrom(....) is a blocking method....