我不确定采用哪种策略...我正在专注于完成我的操作,但我也希望将性能问题降至最低...有一种叫做Execute()的方法必须等待(同步运行),直到操作完成。 这个操作发生在另一个线程上。 有两种实现相同操作的方法...
一种是使用ManualResetEvent
void Execute()
{
taskHandle = new ManualResetEvent(false);
.
.
//delegate task to another thread
.
.
taskHandle.WaitOne();
}
OR
通过使用简单的while结构
void Execute()
{
.
.
//delegate task to another thread
.
.
while (!JobCompleted)
Thread.Sleep(1000);
}
我应该采用这两种方法中的哪一种呢?为什么?
编辑:
Q2. 如果我只有一个空的while循环,那有什么区别吗?
while(!JobCompleted);
编辑:(之前我收集的一些信息)
http://www.yoda.arachsys.com/csharp/threads/waithandles.shtml - 这篇文章说手动重置事件相对较慢,因为它们会跳出托管代码再次进入...