I have a question about synchronisation:
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() executes synchronous (and is a "short running" task, e.g. < 1 sec). The third-party's .Execute method on the other hand executes A-synchronously. I want the method ProcessStuff to be executed synchronously always; so I want it to return when Q.Execute is completed. Q raises the TaskCompleted event when it's done. But I ALSO want to report it's progress.
I can't use a ManualResetEvent
because the .WaitOne()
method will block the current thread and thus block reporting the progress. The ProcessStuff method gets called for each object in a queue and the tasks in that queue need to be executed in-order, non-parallel.
while (MyQueue.Count > 0)
ProcessStuff(MyQueue.Dequeue(), MyProgressDialog);
I am, for this project, stuck with .Net 2.0
It's friday night and am tired so I might have overlooked something but I don't see how to fix this. This works; but I don't think it's the way to go:
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();
}
}
}
Can anyone nudge me in the right direction?