With asynchronous delegate invocation the most useful way to detect completion is to use a callback. This is a basic template which you can adapt:
using System.Runtime.Remoting.Messaging;
private delegate void ProcessingMethodDelegate();
public void StartAsync() {
ProcessingMethodDelegate pm = new ProcessingMethodDelegate(ProcessingMethod);
pm.BeginInvoke(AsyncComplete, null);
}
public void ProcessingMethod () {
}
public void AsyncComplete(IAsyncResult ar) {
AsyncResult result = (AsyncResult)ar;
try {
((ProcessingMethodDelegate)result.AsyncDelegate).EndInvoke(ar);
} catch (Exception e) {
} finally {
}
}
In your case, unless you really have a great desire to use asynchronous delegates, I would suggest the
Backgroundworker class[
^] which has progress and completion events, both of which are raised on the UI thread.
Whatever you choose the next action is initiated from the completion handler and in a UI application this is often nothing more than enabling buttons that were disabled for the duration of the background task.
In response to your comment:
With a sequence of tasks one
must end before the next is allowed to start and your code has to enforce this requirement.
A possible solution is to run the sequence in one thread, i.e. run the whole sequence as one background operation.
e.g.
public void ProcessingMethod () {
Task1();
Task2();
Task3();
}
The alternative is my original suggestion which is to chain the completion handlers of each individual asynchronous task so that completion of one starts the next.
Whether you use Backgroundworker or asynchronous delegates makes no difference to the logical flow of the program, it is just that making it happen correctly is more work for the programmer.
Alan.