1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Help With WaitHandle.WaitAll()

Discussion in 'Visual Basic ( VB )' started by Andrew777, Aug 21, 2007.

  1. Andrew777

    Andrew777 New Member

    Hello,

    I am trying to create a program that executes threads, waits until they are finished and then executes some more code.
    When I run it, I recieve a "Object not set to an instance" error on WaitHandle.WaitAll(Tasks). Why does this occur?, WaitHandle.WaitAll is a static method.

    Hope someone can help


    Code:
    Module Module1
    
    Dim Tasks(1) As AutoResetEvent
    
    <MTAThread()> Sub Main()
            Tasks(0) = New AutoResetEvent(False)
            Tasks(1) = New AutoResetEvent(False)
            Tasks(2) = New AutoResetEvent(False)
    
            ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf T1), Tasks(0))
            ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf T2), Tasks(1))
    
             WaitHandle.WaitAll(Tasks)
            ' Do some Tasks
        End Sub
       
        Sub T1(ByVal state As Object)
            System.Console.Write("In T1")
            Thread.Sleep(3000)
            Tasks(0).Set()
        End Sub
    
        Sub T2(ByVal state As Object)
             System.Console.Write("In T2")
             Thread.Sleep(3000)
             Tasks(1).Set()
        End Sub
    
    End Module
    
    
     
  2. Andrew777

    Andrew777 New Member

    Sorry,
    This Line

    Tasks(2) = New AutoResetEvent(False)

    is not supposed to be there, just ignore it.
     

Share This Page