471,325 Members | 1,320 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Aborting a thread ?

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
Jan 5 '06 #1
3 5856

"jmd.msdn" <jm******@newsgroup.nospam> wrote in message
news:eJ**************@TK2MSFTNGP09.phx.gbl...
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.

Jan 5 '06 #2
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

"Willy Denoyette [MVP]" <wi*************@telenet.be> wrote in message
news:e$**************@tk2msftngp13.phx.gbl...

"jmd.msdn" <jm******@newsgroup.nospam> wrote in message
news:eJ**************@TK2MSFTNGP09.phx.gbl...
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.

Jan 6 '06 #3
Try closing your client before calling thread abort in the thread where
you are calling Abort
jmd.msdn wrote:
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

Jan 6 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Xarky | last post: by
3 posts views Thread by Jeff Greenland | last post: by
2 posts views Thread by Mark Denardo | last post: by
7 posts views Thread by archana | last post: by
reply views Thread by =?Utf-8?B?aGVyYmVydA==?= | last post: by
4 posts views Thread by Mufasa | last post: by
3 posts views Thread by AdrianDev | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.