J
Jim
Hi,
I remember from my c++ days that hard terminating a thread is a bad thing,
as it can cause
resource leaks since destructors and clean up code never gets a chance to
get executed.
I'm writing a multithreaded app in C# that has various threads running loops
that poll sockets.
I want to be able to stop the polling thread from a different thread. As far
as I know, there are two
ways to do this:
1. Create a bool instance member protected by a Mutex that the Stop() method
will set to false. The thread
that needs to be stopped will simply check the value of this bool on each
loop.
2. call the thread's Abort() method.
I would surely like to avoid having to deal with the bool instance member
and the mutex wait/release calls if I could,
but calling Abort just feels naughty. Also, if my thread is in a blocking
socket receive call, it might never loop back around to figure out that it
should terminate. I know the garbage collector should in theory be able to
detect when objects can
be released, but is it completely solid? Also, in regards to sockets, if I
kill a thread that has a socket making a blocking call in it, am I somehow
leaking a socket resource?
-Jim
I remember from my c++ days that hard terminating a thread is a bad thing,
as it can cause
resource leaks since destructors and clean up code never gets a chance to
get executed.
I'm writing a multithreaded app in C# that has various threads running loops
that poll sockets.
I want to be able to stop the polling thread from a different thread. As far
as I know, there are two
ways to do this:
1. Create a bool instance member protected by a Mutex that the Stop() method
will set to false. The thread
that needs to be stopped will simply check the value of this bool on each
loop.
2. call the thread's Abort() method.
I would surely like to avoid having to deal with the bool instance member
and the mutex wait/release calls if I could,
but calling Abort just feels naughty. Also, if my thread is in a blocking
socket receive call, it might never loop back around to figure out that it
should terminate. I know the garbage collector should in theory be able to
detect when objects can
be released, but is it completely solid? Also, in regards to sockets, if I
kill a thread that has a socket making a blocking call in it, am I somehow
leaking a socket resource?
-Jim