Handling Threads

T

tshad

I am writing a Service that will start up some threads and I am trying to
find a way to tell if the threads are still running or not?

Is there a good program that will show that threads a service or program has
running?

Part of my code is:
*****************************************************************************
Protected Overrides Sub OnStart(ByVal args() As String)
Dim oCredit1 As New CreditProcessor
'LogInfo("CreditPoller Started")
myLog.WriteEntry("Demo Service Started @ " & TimeStamp())

myThread1 = New Thread(AddressOf oCredit1.ProcessCredit)
oCredit1.ThreadNumber = "1"
myThread1.Start()
End Sub

Class CreditProcessor
Inherits CreditService
Public ThreadNumber As String

Public Sub ProcessCredit()
If debugging Then
DebugPoller("PollAndHandleCredit() Starting Thread Number: " &
ThreadNumber)
End If

If debugging Then
DebugPoller("PollAndHandleCredit() Exiting Thread Number: " &
ThreadNumber)
End If
End Sub
End Class
*******************************************************************

The program just starts a thread that prints to a file that the thread is
starting and ending.

In this example, when the ProcesCredit() function exits - does that kill the
thread?

If not, how do I do that?

Thanks,

Tom
 
S

Samuel Shulman

What about using variables as flags

These flags will be set to say True just before the thread finishes

The variable must be declared outside the thread

hth,
Samuel
 
M

Mudhead

Check the IsAlive property of the thread.

If myThread1.IsAlive = False then
'-- Do Something
End if
 
T

tshad

Mudhead said:
Check the IsAlive property of the thread.

If myThread1.IsAlive = False then
'-- Do Something
End if

That makes sense.

In this case, if the thread exits normally such that the ThreadState =
Stopped, can I just restart it doing something like:

myThread1 = New Thread(AddressOf oCredit1.ProcessCredit)
oCredit1.ThreadNumber = "1"
myThread1.Start()
myThread1.Start()

Or

myThread1 = New Thread(AddressOf oCredit1.ProcessCredit)
oCredit1.ThreadNumber = "1"
myThread1.Start()
oCredit1.ThreadNumber = "20"
myThread1.Start()


Or do I need to reset the thread - something like:

myThread1 = New Thread(AddressOf oCredit1.ProcessCredit)
oCredit1.ThreadNumber = "1"
myThread1.Start()
myThread1 = New Thread(AddressOf oCredit1.ProcessCredit)
oCredit1.ThreadNumber = "20"
myThread1.Start()

Thanks,

Tom
 
M

Mudhead

Just reinitialize it:

myThread1 = New Thread(AddressOf oCredit1.ProcessCredit)
oCredit1.ThreadNumber = "1"
myThread1.Start()
 
T

tshad

Mudhead said:
Just reinitialize it:

myThread1 = New Thread(AddressOf oCredit1.ProcessCredit)
oCredit1.ThreadNumber = "1"
myThread1.Start()

That was what I thought. It seemed that when I just just redid the
myThread1.Start() without reinitializing it was freezing. Just wanted to
make sure.

Thanks,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top