代码之家  ›  专栏  ›  技术社区  ›  Andrew Arnott

如何在没有消息泵的情况下等待WaitHandle?

  •  10
  • Andrew Arnott  · 技术社区  · 17 年前

    WaitHandle.WaitOne(TimeSpan) 在.NET中,但我在STA线程上,它在等待时发送消息。由于超出这个问题范围的原因,我需要等待 没有 抽吸。我如何等待WaitHandle发出信号而不发送消息?

    在某些情况下,似乎 WaitHandle.WaitOne 不发送消息。但有时对某些信息来说确实如此。有关这方面的更多信息,请参见以下链接:

    1. Managed and Unmanaged Threading in Microsoft Windows
    2. Managed blocking
    3 回复  |  直到 17 年前
        1
  •  9
  •   x0n    17 年前

    WaitForSingleObject或WaitForMultipleObject是非泵送等待;使用p/invoke。

    [DllImport("kernel32", SetLastError=true, ExactSpelling=true)]
    public static extern Int32 WaitForSingleObject(SafeWaitHandle handle, Int32 milliseconds);
    

    -奥辛

        2
  •  3
  •   russilwvong    14 年前

    显然,这是一个 change introduced in Microsoft Vista : CoWaitForMultipleHandles WM_PAINT 信息。

    Vista bug report

        3
  •  1
  •   Cameron    9 年前

    如果您在WPF应用程序中,调度程序在托管等待期间运行消息泵,那么在等待期间禁用消息泵的最简单方法是通过 Dispatcher.DisableProcessing :

    // The Dispose() method is called at the end of the using statement. 
    // Calling Dispose on the DispatcherProcessingDisabled structure,  
    // which is returned from the call to DisableProcessing, will 
    // re-enable Dispatcher processing. 
    using (Dispatcher.CurrentDispatcher.DisableProcessing())
    {
        // Do work while the dispatcher processing is disabled.
        Thread.Sleep(2000);
    }
    
    推荐文章