1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Help With WaitHandle.WaitAll()

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

  1. Andrew777

    Andrew777 New Member

    Joined:
    Aug 21, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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

    Joined:
    Aug 21, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Sorry,
    This Line

    Tasks(2) = New AutoResetEvent(False)

    is not supposed to be there, just ignore it.
     

Share This Page