|
-
Oct 16th, 2006, 03:02 PM
#1
Thread Starter
Lively Member
[RESOLVED] check if a thread is still running?
I would like to know how to check and see if a thread is still running or not.
for example
VB Code:
private sub mysub1
dim thread1 as new system.threading.thread(addressof mythread1)
'i only want to start the thread if it isnt allready running
'if thread1 isnt running then
thread1.start
'end if
end sub
private sub mythread1
do while a = true
some code here
loop
end sub
Last edited by chaos75; Oct 16th, 2006 at 03:15 PM.
-
Oct 16th, 2006, 03:10 PM
#2
Re: check if a thread is still running?
It couldnt be running since you declare it right before checking, in the same sub. Or am I wrong?
Otherwise its:
VB Code:
If Not thread1.IsAlive
thread1.Start
End If
-
Oct 16th, 2006, 03:21 PM
#3
Thread Starter
Lively Member
Re: check if a thread is still running?
in that example im assuming that the thread had been started once allready. what the program is doing is reading a log file and checking if the last line is = to a string that i set. if it is it will set "a" to true and start the thread "thread1" if it isnt running yet. the log is changing all the time so the "thread1" thread should run untill another line in the log is read that is = to another string i set and set a to false. but is the isalive is what you use to check if the thread is running or not then you answered my question.
would this be the same as isalive?
VB Code:
If thread1.ThreadState = ThreadState.Running = False Then
dim thread1 as new system.threading.thread(addressof mythread)
thread1.start
end if
-
Oct 16th, 2006, 03:33 PM
#4
Re: check if a thread is still running?
You are creating a new instance of the thread every time, therefore IsAlive will always return false. What I think you should do is declare it outside the sub, in general declarations. Then just check if its alive, and if its not start it up.
If you take a look at just these two lines:
VB Code:
If thread1.ThreadState = ThreadState.Running = False Then
dim thread1 as new system.threading.thread(addressof mythread)
You see that youre checking an object that you havent declared yet. That wont work :P
Id suggest you do like this:
VB Code:
'Declare this OUTSIDE any sub.
Dim thread1 as New System.Threading.Thread(addressof mythread)
private sub mysub1
If Not thread1.IsAlive Then
thread1 = New System.Threading.Thread(addressof mythread)
thread1.Start()
End If
End Sub
Last edited by Atheist; Oct 17th, 2006 at 02:43 AM.
-
Oct 16th, 2006, 03:42 PM
#5
Thread Starter
Lively Member
Re: check if a thread is still running?
Ok thanx ill give that a shot
-
Oct 16th, 2006, 03:53 PM
#6
Thread Starter
Lively Member
Re: [RESOLVED] check if a thread is still running?
ok that worked better, the code is much neater then what i had too.
thanx again
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|