【发布时间】:2010-07-16 16:18:40
【问题描述】:
我有一个关于同步的问题:
private void ProcessStuff(Task sometask, Form progressform)
{
if (sometask.foo == "A")
DoStuff(); //This one is SYNchronous
else
{
ThirdPartyObject Q = new ThirdPartyObject();
Q.ProgessChanged += delegate(object sender, ProgressChangedEventArgs e) {
progressform.ShowProgress(e.ProgressPercentage);
};
Q.TaskCompleted += delegate(object sender, TaskResult result) { /* ??? */ };
Q.Execute("X", "Y", "Z"); //This one executes ASYNchronous
}
}
DoStuff() 同步执行(并且是“短期运行”任务,例如
我不能使用ManualResetEvent,因为.WaitOne() 方法会阻塞当前线程,从而阻塞报告进度。 ProcessStuff 方法为队列中的每个对象调用,并且该队列中的任务需要按顺序、非并行执行。
while (MyQueue.Count > 0)
ProcessStuff(MyQueue.Dequeue(), MyProgressDialog);
对于这个项目,我坚持使用 .Net 2.0
现在是星期五晚上,我很累,所以我可能忽略了一些东西,但我不知道如何解决这个问题。这行得通;但我不认为这是要走的路:
private void ProcessStuff(Task sometask, Form progressform)
{
...
Q.TaskCompleted += delegate(object sender, TaskResult result) { /* ??? */ };
Q.Execute("X", "Y", "Z"); //This one executes ASYNchronous
while (Q.IsBusy) {
//I Could Thread.Sleep(10) here...
Application.DoEvents();
}
}
}
谁能把我推向正确的方向?
【问题讨论】:
标签: c# winforms asynchronous