Results 1 to 6 of 6

Thread: [RESOLVED] check if a thread is still running?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    104

    Resolved [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:
    1. private sub mysub1
    2. dim thread1 as new system.threading.thread(addressof mythread1)
    3.  
    4. 'i only want to start the thread if it isnt allready running
    5.  
    6. 'if thread1 isnt running then
    7. thread1.start
    8. 'end if
    9. end sub
    10.  
    11. private sub mythread1
    12. do while a = true
    13. some code here
    14. loop
    15. end sub
    Last edited by chaos75; Oct 16th, 2006 at 03:15 PM.

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. If Not thread1.IsAlive
    2.     thread1.Start
    3. End If
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    104

    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:
    1. If thread1.ThreadState = ThreadState.Running = False Then
    2. dim thread1 as new system.threading.thread(addressof mythread)
    3. thread1.start
    4. end if

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. If thread1.ThreadState = ThreadState.Running = False Then
    2. 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:
    1. 'Declare this OUTSIDE any sub.
    2. Dim thread1 as New System.Threading.Thread(addressof mythread)
    3.  
    4. private sub mysub1
    5. If Not thread1.IsAlive Then
    6.     thread1 = New System.Threading.Thread(addressof mythread)
    7.     thread1.Start()
    8. End If
    9. End Sub
    Last edited by Atheist; Oct 17th, 2006 at 02:43 AM.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    104

    Re: check if a thread is still running?

    Ok thanx ill give that a shot

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    104

    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
  •  



Click Here to Expand Forum to Full Width