Results 1 to 4 of 4

Thread: checking thread completion before continuing.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    184

    checking thread completion before continuing.

    I'm having trouble checking the completion of multiple threads before executing more code.

    As an example, I started two threads:

    **************************
    Dim Thread1 As New System.Threading.Thread(AddressOf wordpress_checker_thread)
    Thread1.Start()
    Dim Thread2 As New System.Threading.Thread(AddressOf wordpress_checker_thread)
    Thread2.Start()

    (check if Thread1 and Thread2 finish running)

    * continue with more code.
    *******************

    I'd like to check when these threads are completed.

    I've used If Thread1.IsAlive in a timer but that didn't work because I can't pass Thread1 to the Timer handler. I've declared Thread1 globally using public, it does work but only once because when I rerun the code, you can't restart the same Thread twice.

    Any suggestion on how to implement this.

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: checking thread completion before continuing.

    What I would do is to encapsulate those threading operations inside a class. I'd give this class completion type events which I would raise when the thread has completed. Would you like to see an example of how I'd approach it ?
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    184

    Re: checking thread completion before continuing.

    Niya, I would appreciate an example. Thx.

  4. #4
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: checking thread completion before continuing.

    Sorry I been so long. Was a lil busy cooking. Here is an example of how I implement threading in my components:-
    vbnet Code:
    1. Imports System.Threading
    2.  
    3. Public Class Counter
    4.  
    5.     'Incase we need to update anything on the UI
    6.     'we use this control to invoke events
    7.     'on the UI thread
    8.     Private g_objCtl As Control
    9.  
    10.     Private g_thrdInternalBeginCount As Thread
    11.  
    12.     Public Event CountChanged As EventHandler(Of CountChangedEventArgs)
    13.     Public Event CountCompleted As EventHandler
    14.  
    15.     Protected Overridable Sub OnCountChanged(ByVal e As CountChangedEventArgs)
    16.         RaiseEvent CountChanged(Me, e)
    17.     End Sub
    18.     Protected Overridable Sub OnCountCompleted()
    19.         RaiseEvent CountCompleted(Me, New EventArgs)
    20.     End Sub
    21.    
    22.  
    23.     Public Sub BeginCountAsync(ByVal max As Integer, ByVal delay As Integer)
    24.  
    25.         'Only one instance of the asyncronous counting is allowed
    26.         If g_thrdInternalBeginCount IsNot Nothing AndAlso g_thrdInternalBeginCount.IsAlive Then
    27.             Throw New NotSupportedException("BeginCount already running")
    28.         End If
    29.  
    30.         'Create the control on the thread that called this function
    31.         'so we can use the control to invoke on said thread
    32.         InitThreading()
    33.  
    34.         g_thrdInternalBeginCount = New Thread(AddressOf InternalBeginCount)
    35.  
    36.         'Ensures that this thread terminates
    37.         'when the application terminates
    38.         g_thrdInternalBeginCount.IsBackground = True
    39.  
    40.  
    41.         g_thrdInternalBeginCount.Start(New InternalBeginCountArgs With {.Delay = delay, .Max = max})
    42.     End Sub
    43.  
    44.     Public Sub BeginCount(ByVal max As Integer, ByVal delay As Integer)
    45.         InternalBeginCount(max, delay)
    46.     End Sub
    47.  
    48.     'Thread.Start is only able to take 1 parameter so we must
    49.     'package the parameters in an object
    50.     Private Sub InternalBeginCount(ByVal args As InternalBeginCountArgs)
    51.         InternalBeginCount(args.Max, args.Delay)
    52.     End Sub
    53.  
    54.     Private Sub InternalBeginCount(ByVal Max As Integer, ByVal Delay As Integer)
    55.  
    56.         For i = 1 To Max
    57.  
    58.             'If this function was called on a different thread
    59.             'from the thread the control was created on
    60.             'then we need to invoke it on the control's thread
    61.             If g_objCtl.InvokeRequired Then
    62.                 g_objCtl.Invoke(New Action(Of CountChangedEventArgs)(AddressOf OnCountChanged), New CountChangedEventArgs(i, Max))
    63.             Else
    64.                 OnCountChanged(New CountChangedEventArgs(i, Max))
    65.             End If
    66.  
    67.             thread.sleep(Delay)
    68.         Next
    69.  
    70.         If g_objCtl.InvokeRequired Then
    71.             g_objCtl.Invoke(New Action(AddressOf OnCountCompleted))
    72.         Else
    73.             OnCountCompleted()
    74.         End If
    75.  
    76.     End Sub
    77.  
    78.     Private Sub InitThreading()
    79.         If g_objCtl IsNot Nothing Then g_objCtl.Dispose()
    80.  
    81.         g_objCtl = New Control
    82.  
    83.         'Force creation of handle on current thread
    84.         Dim x As IntPtr = g_objCtl.Handle
    85.  
    86.     End Sub
    87.  
    88.     Private Class InternalBeginCountArgs
    89.         Public Max As Integer
    90.         Public Delay As Integer
    91.     End Class
    92.  
    93. End Class
    94.  
    95. Public Class CountChangedEventArgs
    96.     Inherits EventArgs
    97.  
    98.     Private _iMax As Integer
    99.     Private _iCurCount As Integer
    100.  
    101.  
    102.     Public Sub New(ByVal curCount As Integer, ByVal max As Integer)
    103.         _iMax = max
    104.         _iCurCount = curCount
    105.     End Sub
    106.     Public ReadOnly Property CurCount() As Integer
    107.         Get
    108.             Return _iCurCount
    109.         End Get
    110.     End Property
    111.     Public ReadOnly Property Max() As Integer
    112.         Get
    113.             Return _iMax
    114.         End Get
    115.     End Property
    116.  
    117. End Class
    I tried to make this as simple as possible for you. Its simple a class that counts to a specified number and raises two events. One event is raised every time the counter is incremented and another event is raised on completion of the count. To test it, place the above code in a class file and place a Label control and a Button control on a Form and use this code:-
    vbnet Code:
    1. Public Class Form1
    2.     Private WithEvents counter As New Counter
    3.  
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         counter.BeginCountAsync(100, 100)
    6.     End Sub
    7.  
    8.     Private Sub counter_CountChanged(ByVal sender As Object, ByVal e As CountChangedEventArgs) Handles counter.CountChanged
    9.         Label1.Text = CStr(e.CurCount)
    10.     End Sub
    11.  
    12.     Private Sub counter_CountCompleted(ByVal sender As Object, ByVal e As System.EventArgs) Handles counter.CountCompleted
    13.         MsgBox("Count complete")
    14.     End Sub
    15.  
    16.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    17.  
    18.     End Sub
    19. End Class
    If you have any questions about it let me know.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

Tags for this Thread

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