none
MSDN示例运行出错.. 不支持一个 STA 线程上多个句柄的 WaitAll RRS feed

  • 问题

  • 晕了,msdn上的示例也是相同的提示..
    代码如下:
    Imports System
    Imports System.Threading
    
    Module Module1
    
        Sub Main()
            Dim a As New App
            App.Main()
            Console.ReadKey()
        End Sub
    
    End Module
    
    Public NotInheritable Class App
        ' Define an array with two AutoResetEvent WaitHandles.
        Private Shared waitHandles() As WaitHandle = _
            {New AutoResetEvent(False), New AutoResetEvent(False)}
    
        ' Define a random number generator for testing.
        Private Shared r As New Random()
    
        <MTAThreadAttribute()> _
        Public Shared Sub Main()
            ' Queue two tasks on two different threads; 
            ' wait until all tasks are completed.
            Dim dt As DateTime = DateTime.Now
            Console.WriteLine("Main thread is waiting for BOTH tasks to complete.")
            ThreadPool.QueueUserWorkItem(AddressOf DoTask, waitHandles(0))
            ThreadPool.QueueUserWorkItem(AddressOf DoTask, waitHandles(1))
            WaitHandle.WaitAll(waitHandles)
            ' The time shown below should match the longest task.
            Console.WriteLine("Both tasks are completed (time waited={0})", _
                (DateTime.Now - dt).TotalMilliseconds)
    
            ' Queue up two tasks on two different threads; 
            ' wait until any tasks are completed.
            dt = DateTime.Now
            Console.WriteLine()
            Console.WriteLine("The main thread is waiting for either task to complete.")
            ThreadPool.QueueUserWorkItem(AddressOf DoTask, waitHandles(0))
            ThreadPool.QueueUserWorkItem(AddressOf DoTask, waitHandles(1))
            Dim index As Integer = WaitHandle.WaitAny(waitHandles)
            ' The time shown below should match the shortest task.
            Console.WriteLine("Task {0} finished first (time waited={1}).", _
                index + 1, (DateTime.Now - dt).TotalMilliseconds)
    
        End Sub 'Main
    
        Shared Sub DoTask(ByVal state As [Object])
            Dim are As AutoResetEvent = CType(state, AutoResetEvent)
            Dim time As Integer = 1000 * r.Next(2, 10)
            Console.WriteLine("Performing a task for {0} milliseconds.", time)
            Thread.Sleep(time)
            are.Set()
    
        End Sub 'DoTask
    End Class 'App
    
    ' This code produces output similar to the following:
    '
    '  Main thread is waiting for BOTH tasks to complete.
    '  Performing a task for 7000 milliseconds.
    '  Performing a task for 4000 milliseconds.
    '  Both tasks are completed (time waited=7064.8052)
    ' 
    '  The main thread is waiting for either task to complete.
    '  Performing a task for 2000 milliseconds.
    '  Performing a task for 2000 milliseconds.
    '  Task 1 finished first (time waited=2000.6528).
    
    
    
    不支持一个 STA 线程上多个句柄的 WaitAll
    补充一下,以上是放在控制台程序中的,运行环境是VB2005,不会是我的IDE某个环境变量没设置好吧??
    2009年9月2日 5:57

答案

  • 因为程序调用的不是做标记的那个main


    只要修改成为

    Module Module1
      <MTAThreadAttribute()> _
        Sub Main()
            Dim a As New App
            App.Main()
            Console.ReadKey()
        End Sub

    End Module

      
    就通过了


    恭喜自己5星用户达成
    • 已标记为答案 picat 2009年9月2日 6:41
    2009年9月2日 6:34

全部回复

  • 因为程序调用的不是做标记的那个main


    只要修改成为

    Module Module1
      <MTAThreadAttribute()> _
        Sub Main()
            Dim a As New App
            App.Main()
            Console.ReadKey()
        End Sub

    End Module

      
    就通过了


    恭喜自己5星用户达成
    • 已标记为答案 picat 2009年9月2日 6:41
    2009年9月2日 6:34
  • 嗯,可以了~~
    关于<MTAThreadAttribute()> 和<STAThreadAttribute()> msdn上介绍非常少,不知道该到哪去找点相关资料..
    2009年9月2日 6:42