如何使用处理消息队列来执行 Monitor.Wait?

How can I do a Monitor.Wait with processing message queue?

我正在使用 .NET 4.0(无异步)。

我正在寻找 Monitor.Wait and Application.Run 的组合。我浏览过对象资源管理器,但找不到。

它存在吗?如果没有,最好的实现方式是什么?

我仍然没有尝试过,因为我的想法很脏。像这样:

while (!(timeOutHasBeenReached || aLockHasBeenAcquired))
{
    Application.DoEvents() ; 
}

为了避免XY问题,我介绍一下最终目标(与单元测试有关):

using (ApplicationContext applicationContext = new ApplicationContext()) 
{
    using (Control control = new Control() { Visible = true } )
    {
        // control will do something across the web 
        // When the control has done it, it will raise an event invoking applicationContext.Exit() 
        // As a web connection is not always timing trustfull I'd like 
        // to specify a timeout (or eventually abort it with a Monitor.Pulse)

        Application.Run(applicationContext);         
    }
}

Control 是我无法修改的 Winform 组件,我也不想这样做 Thread.Abort

你不能通过 control.Invoke(() => Application.Exit()); 中止吗?或者只是 control.Close();Dispose.