【发布时间】:2015-12-08 17:00:45
【问题描述】:
我正在使用 .NET 4.0(无异步)。
我正在寻找Monitor.Wait 和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 。
【问题讨论】:
-
Application.DoEvents() 通常(并且多次错误地)用于“避免”多线程。那么你到底想做什么呢?
-
@yms : 我做了一个编辑
-
你不能通过
control.Invoke(() => Application.Exit());中止吗?或者只是control.Close();或 Dispose。 -
@usr :是的,我可以做到。谢谢
标签: c# multithreading winforms monitor