multithreading vb.net - how do I tell when a thread is done

S

Steven Thomas

I have an application that creates mutliple thread like this:

My code:
-------------------------------------------------------------------------
Private Sub StartGPReportServer()
Try
'Dim some variables........
While IsRunning
' LOOP THROUGH THE AVAILABLE THREADS
' I HAVE AN ARRAY WITH A RECORD FOR EACH THREAD THE
APPLICATION
' CAN CREATE. IN THIS CASE 6
While c < threads
' CHECK TO SEE IF THERE ARE REPORTS TO BE RUN
If arThreadCnts(3, c) = 0 Then
'POLL THE QUEUE TO LOOK FOR WORK
dtReportToRun = cFunctions.polldb(arQueues(x),
arThreadCnts(0, c))
' IF THERE IS WORK TO BE DONE READ THE DATA
FROM THE REPORTQUEUE
If CInt(dtReportToRun.Rows.Count) > 0 Then
arThreadCnts(3, c) = 1
'CInt(arThreadCnts(3,c)) + 1
'READ THE DATA ABOUT THE REQUEST
Dim cAccessFunc as new clsAccessFunctions
Dim othread1 As New Thread(AddressOf
cAccessFunc.CAccessSnapShot)

'Create the new thread
'SET THE PROPERTIES OF THE SUB
othread1.Name = CStr(arThreadCnts(1,
c))
cAccessFunc.pQConnectionString =
arQueues(x)
cAccessFunc.pInputFileName =
CStr(dtReportToRun.Rows(0).Item("InputFileName"))
.........
'Start the thread
othread1.start()
End If
End If
c += 1
End While
c = 0
Thread.CurrentThread.Sleep(1000)
End While
Catch errorVariable As Exception
'Error trapping
cFunctions.WriteEventLog("GPReport Could not start : " &
CStr(System.DateTime.Now()) & " " & errorVariable.Message.ToString)
End Try
End Sub

I have this setup to run 6 threads at a time. How can I tell when a
thread has finished?
 
G

Guest

Hi Steven Thomas

U can use ThreadState property of the thread class and find out what is the current status of the thread. It is a enumeration of thread states. I hope this will work

With regards

Sadha Sivam S
 
C

Cor Ligthert

Hi Steven,

Maybe others have other answers, however in your scenario I have a class
with at the end of every thread procedure the function.

ProcessThreadReady("Threadnumber")

I hope this helps,

Cor
 
G

Guest

Do you need to wait for all of your threads to finish? Then you could use Thread.Join. Alternatively your thread could raise an event or signal using the auto/manualresetevent classes

Da

----- Steven Thomas wrote: ----

I have an application that creates mutliple thread like this

My code
------------------------------------------------------------------------
Private Sub StartGPReportServer(
Tr
'Dim some variables.......
While IsRunnin
' LOOP THROUGH THE AVAILABLE THREAD
' I HAVE AN ARRAY WITH A RECORD FOR EACH THREAD TH
APPLICATIO
' CAN CREATE. IN THIS CASE
While c < thread
' CHECK TO SEE IF THERE ARE REPORTS TO BE RU
If arThreadCnts(3, c) = 0 The
'POLL THE QUEUE TO LOOK FOR WOR
dtReportToRun = cFunctions.polldb(arQueues(x)
arThreadCnts(0, c)
' IF THERE IS WORK TO BE DONE READ THE DAT
FROM THE REPORTQUEU
If CInt(dtReportToRun.Rows.Count) > 0 The
arThreadCnts(3, c) =
'CInt(arThreadCnts(3,c)) +
'READ THE DATA ABOUT THE REQUES
Dim cAccessFunc as new clsAccessFunction
Dim othread1 As New Thread(AddressO
cAccessFunc.CAccessSnapShot
'SET THE PROPERTIES OF THE SU
othread1.Name = CStr(arThreadCnts(1
c)
cAccessFunc.pQConnectionString
arQueues(x
cAccessFunc.pInputFileName
CStr(dtReportToRun.Rows(0).Item("InputFileName")
........
'Start the threa
othread1.start(
End I
End I
c +=
End Whil
c =
Thread.CurrentThread.Sleep(1000
End Whil
Catch errorVariable As Exceptio
'Error trappin
cFunctions.WriteEventLog("GPReport Could not start : " &> CStr(System.DateTime.Now()) & " " & errorVariable.Message.ToString
End Tr
End Su

I have this setup to run 6 threads at a time. How can I tell when
thread has finished
 
S

Steven Thomas

Cor Ligthert said:
Hi Steven,

Maybe others have other answers, however in your scenario I have a class
with at the end of every thread procedure the function.

ProcessThreadReady("Threadnumber")

I hope this helps,

Cor

Ok this is what I did to make it all work
1) in the class where the work is being done I create an event
Public Event ThreadDone(ByVal DBType As String, ByVal ThreadName
As String)
2) at the end of the sub that does the work I raise that event
RaiseEvent ThreadDone(pDBType, vThreadName)
3) In my main code I have a sub to catch that event
Sub RptMSAThreadDone(ByVal pDBType As String, ByVal pThreadName As
String) Handles cAccessFunc.ThreadDone
RecoverThreadCnt(pDBType, pThreadName)
End Sub
4) I linked the two together when I was setting up the thread
Dim cAccessFunc As New AccessFunc()
'ADD A HANDLER TO CAPTURE DATA BACK FROM THE THREAD
AddHandler cAccessFunc.ThreadDone, AddressOf RptMSAThreadDone
Dim othread1 As New Thread(AddressOf cAccessFunc.CAccessSnapShot)
.......

Well it works.
 
C

Cor Ligthert

Hi Steven,

When I understand you well than that is almost the same as I do in the
section which does that ProcessThreadReady("Threadnumber") I sand you. It
looks if you had copied that routine which I did not send you. (I have also
two parameters and not one).

:)

Cor
 

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