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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steven Thomas

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

    I have an application that creates mutliple thread like this:

    My code:[color=blue]
    > -------------------------------------------------------------------------
    > Private Sub StartGPReportSe rver()
    > 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.poll db(arQueues(x),
    > arThreadCnts(0, c))
    > ' IF THERE IS WORK TO BE DONE READ THE DATA
    > FROM THE REPORTQUEUE
    > If CInt(dtReportTo Run.Rows.Count) > 0 Then
    > arThreadCnts(3, c) = 1
    > 'CInt(arThreadC nts(3,c)) + 1
    > 'READ THE DATA ABOUT THE REQUEST
    > Dim cAccessFunc as new clsAccessFuncti ons
    > Dim othread1 As New Thread(AddressO f
    > cAccessFunc.CAc cessSnapShot)
    >
    > 'Create the new thread
    > 'SET THE PROPERTIES OF THE SUB
    > othread1.Name = CStr(arThreadCn ts(1,
    > c))
    > cAccessFunc.pQC onnectionString =
    > arQueues(x)
    > cAccessFunc.pIn putFileName =
    > CStr(dtReportTo Run.Rows(0).Ite m("InputFileNam e"))
    > .........
    > 'Start the thread
    > othread1.start( )
    > End If
    > End If
    > c += 1
    > End While
    > c = 0
    > Thread.CurrentT hread.Sleep(100 0)
    > End While
    > Catch errorVariable As Exception
    > 'Error trapping
    > cFunctions.Writ eEventLog("GPRe port Could not start : " &
    > CStr(System.Dat eTime.Now()) & " " & errorVariable.M essage.ToString )
    > End Try
    > End Sub[/color]

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

    #2
    RE: multithreading vb.net - how do I tell when a thread is done

    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

    Comment

    • Cor Ligthert

      #3
      Re: multithreading vb.net - how do I tell when a thread is done

      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.

      ProcessThreadRe ady("Threadnumb er")

      I hope this helps,

      Cor


      Comment

      • Dan Kelley

        #4
        RE: multithreading vb.net - how do I tell when a thread is done

        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/manualreseteven t classes

        Da

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

        I have an application that creates mutliple thread like this

        My code[color=blue]
        > ------------------------------------------------------------------------
        > Private Sub StartGPReportSe rver(
        > 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.poll db(arQueues(x)
        > arThreadCnts(0, c)
        > ' IF THERE IS WORK TO BE DONE READ THE DAT
        > FROM THE REPORTQUEU
        > If CInt(dtReportTo Run.Rows.Count) > 0 The
        > arThreadCnts(3, c) =
        > 'CInt(arThreadC nts(3,c)) +
        > 'READ THE DATA ABOUT THE REQUES
        > Dim cAccessFunc as new clsAccessFuncti on
        > Dim othread1 As New Thread(AddressO
        > cAccessFunc.CAc cessSnapShot[color=green]
        >> 'Create the new threa[/color]
        > 'SET THE PROPERTIES OF THE SU
        > othread1.Name = CStr(arThreadCn ts(1
        > c)
        > cAccessFunc.pQC onnectionString
        > arQueues(x
        > cAccessFunc.pIn putFileName
        > CStr(dtReportTo Run.Rows(0).Ite m("InputFileNam e")
        > ........
        > 'Start the threa
        > othread1.start(
        > End I
        > End I
        > c +=
        > End Whil
        > c =
        > Thread.CurrentT hread.Sleep(100 0
        > End Whil
        > Catch errorVariable As Exceptio
        > 'Error trappin
        > cFunctions.Writ eEventLog("GPRe port Could not start : " &> CStr(System.Dat eTime.Now()) & " " & errorVariable.M essage.ToString
        > End Tr
        > End Su[/color]

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

        Comment

        • Steven Thomas

          #5
          Re: multithreading vb.net - how do I tell when a thread is done

          "Cor Ligthert" <notfirstname@p lanet.nl> wrote in message news:<ubJzsPTSE HA.4032@TK2MSFT NGP11.phx.gbl>. ..[color=blue]
          > 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.
          >
          > ProcessThreadRe ady("Threadnumb er")
          >
          > I hope this helps,
          >
          > Cor[/color]

          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(ByVa l DBType As String, ByVal ThreadName
          As String)
          2) at the end of the sub that does the work I raise that event
          RaiseEvent ThreadDone(pDBT ype, vThreadName)
          3) In my main code I have a sub to catch that event
          Sub RptMSAThreadDon e(ByVal pDBType As String, ByVal pThreadName As
          String) Handles cAccessFunc.Thr eadDone
          RecoverThreadCn t(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.Thr eadDone, AddressOf RptMSAThreadDon e
          Dim othread1 As New Thread(AddressO f cAccessFunc.CAc cessSnapShot)
          .......

          Well it works.

          Comment

          • Cor Ligthert

            #6
            Re: multithreading vb.net - how do I tell when a thread is done

            Hi Steven,

            When I understand you well than that is almost the same as I do in the
            section which does that ProcessThreadRe ady("Threadnumb er") 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


            Comment

            Working...