我有關於同步的問題:)C#同步
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(執行同步(並且是 「短時間運行」 的任務,例如< 1秒)。另一方面,第三方的.Execute方法同步執行A。我希望ProcessStuff方法始終同步執行;所以我希望它在Q.Execute完成時返回。完成後,Q會引發TaskCompleted事件。但我也想報告它的進展。
我不能使用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();
}
}
}
任何人都可以推動我在正確的方向嗎?