private void MyTaskCompletedCallback(IAsyncResult ar) { // get the original worker delegate and the AsyncOperation instance MyTaskWorkerDelegate worker = (MyTaskWorkerDelegate)((AsyncResult)ar).AsyncDelegate; AsyncOperation async = (AsyncOperation)ar.AsyncState; bool cancelled; // finish the asynchronous operation worker.EndInvoke(out cancelled, ar); // clear the running task flag lock (_sync) { _myTaskIsRunning = false; _myTaskContext = null; } // raise the completed event MyTaskAsyncCompletedEventArgs completedArgs = new MyTaskAsyncCompletedEventArgs(null, cancelled, null, Id); async.PostOperationCompleted(delegate(object e) { OnMyTaskCompleted((MyTaskAsyncCompletedEventArgs)e); }, completedArgs); }
protected virtual void OnMyTaskCompleted(MyTaskAsyncCompletedEventArgs e) { if (MyTaskCompleted != null) MyTaskCompleted(this, e); }
static void task_MyTaskCompleted(object sender, MyTaskAsyncCompletedEventArgs e) { Console.WriteLine("Main : task[{0}] completed :) ", e.Id); }