How to check Thread is terminated?

  • Thread starter Thread starter sleepyant
  • Start date Start date
S

sleepyant

Hi, I have a thread where it will do a Do While..Loop and check the
blnCancel boolean in every loop. If blnCancel is True, then the loop will
exit and the Thread should be terminated naturally. But there is some
scenario where the program stuck somewhere in the Do While..Loop and and
unable to exit the loop. In this case, I would need to abort the thread all
together. But I have some problem in doing this, here is how I did:

Private Sub DoCancel()
blnCancel = True

If th.IsAlive Then
If th.Join(10000) = False Then <------Prolem: Always
return False.
th.Abort()
End If
End If
End Sub

The th.Join (miliseconds) always return false even I know the thread should
have terminated "normally". How can I solve this? Please advice. Thanks.
 
sleepyant said:
Hi, I have a thread where it will do a Do While..Loop and check the
blnCancel boolean in every loop. If blnCancel is True, then the loop will
exit and the Thread should be terminated naturally. But there is some
scenario where the program stuck somewhere in the Do While..Loop and and
unable to exit the loop. In this case, I would need to abort the thread all
together. But I have some problem in doing this, here is how I did:

Private Sub DoCancel()
blnCancel = True

If th.IsAlive Then
If th.Join(10000) = False Then <------Prolem: Always
return False.
th.Abort()
End If
End If
End Sub

The th.Join (miliseconds) always return false even I know the thread should
have terminated "normally". How can I solve this? Please advice. Thanks.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Note that just setting a boolean field isn't a thread-safe way of
indicating that a thread should stop, unless the field is volatile.
Otherwise, you need locking.
See http://www.pobox.com/~skeet/csharp/threads/shutdown.shtml
 
Back
Top