473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to check Thread is terminated?

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.
Jul 21 '05 #1
1 1959
sleepyant <no@nono.no> wrote:
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/t...shutdown.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: | last post by:
Assume that we have two applications running on the same PC each with two threads. Application-1, Thread-1 : . . . while(!Terminated && !MYSIGNAL) { //?? How to implement following line ??...
14
by: Daisy | last post by:
From this page: http://www.c-sharpcorner.com/2/mt_beginner1.asp Thread class's Abort method is called to kill a thread permanently. Make sure you call IsAlive before Abort. if (...
10
by: Fernando Rodríguez | last post by:
Hi, Assume I have Main function that looks like this: static void Main(string args) { Action h1 = new Action(0);
7
by: Microsoft | last post by:
I'm trying to use threading for the first time and can't get it to work. (VB) In the page I've got: Sub Page_Load( sender As Object, e As EventArgs ) dim o as new Example.Test...
1
by: sleepyant | last post by:
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...
6
by: cedric | last post by:
hello, I encounter a problem using threads. I have a simple code to use a thread : cSurveillanceBorne = New clsThreadSurveillance _SurveillanceThread = New Thread(AddressOf...
0
by: Sir Spamallot | last post by:
Hi there, Previously when handling the termination of threads I had just called the abort method and caught it in a try catch block in the thread callback. After recently learning that this was...
11
by: Jon Slaughter | last post by:
Is there any way to start a terminated thread without using a pool or creating a new thread object? void counter() { clicks = 0; clock.Start(); while (counterActive) { clicks++;
18
by: =?Utf-8?B?VGhlU2lsdmVySGFtbWVy?= | last post by:
Because C# has no native SSH class, I am using SharpSSH. Sometimes, for reasons I do not know, a Connect call will totally lock up the thread and never return. I am sure it has something to do...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.