Aborting a thread ?

J

jmd.msdn

Hello.
I want to abort a thread whose thread function contains something like :
void threadfunc ()
{
while ( 1 )
{
... some code
udpClient.Receive(...); // instance to read network
data
... some code using received data
}
}

Another method in my thread class has :
thread.Abort();

Problem when Abort() has been called : the thread, as seen in debug mode
shows :
1. do NOT abort
2. remains in an AbortRequested ThreadState
3. IsAlive is true

Question :
Does someone can tell me what can maintains a thread in an
AbortRequested ThreadState ?

Thank you.
jmd
 
W

Willy Denoyette [MVP]

jmd.msdn said:
Hello.
I want to abort a thread whose thread function contains something like :
void threadfunc ()
{
while ( 1 )
{
... some code
udpClient.Receive(...); // instance to read network
data
... some code using received data
}
}

Another method in my thread class has :
thread.Abort();

Problem when Abort() has been called : the thread, as seen in debug mode
shows :
1. do NOT abort
2. remains in an AbortRequested ThreadState
3. IsAlive is true

Question :
Does someone can tell me what can maintains a thread in an
AbortRequested ThreadState ?

Thank you.
jmd

1. Never call Thread.Abort, this method is only safe when you intend to
abandon the current Application domain, in which case it's not needed to
call Abort anyway because AppDomain.Unload calls Abort when it uloads the
offending domain.
2. When you need to end a thread function you should set an event or set a
status flag that you can check in your while loop.

But this won't solve your problem as it's related to the Receive call which
is suspended (waiting for data) in unmanaged code, all you can do is close
udpClient's underlying socket, this will force the Receive to end and give
you a chance to exit the loop.

Willy.
 
J

jmd.msdn

Thank you very much gor your speedy response.
Yes, as you say, I usually stop a thread with a flag and a while ( !
stopwork ) loop.
This one was to test the receive udpclient method.
I will follow your suggestion tomorrow to stop the thread with :
udpclient.Client.Close()

jean-marie
 
J

John Murray

Try closing your client before calling thread abort in the thread where
you are calling Abort
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top