static void Done(IAsyncResult cookie) { WorkInvoker method = (WorkInvoker)cookie.AsyncState; //info about asynch operation int result = method.EndInvoke(cookie); Console.WriteLine("String II length is: " + result); }
/// <summary> /// Implementation of the async work invoker /// </summary> /// <remarks> /// We're using the delegate BeginInvoke() / EndInvoke() pattern here /// </remarks> protected override void BeginInvokeCore() { WorkInvoker worker = new WorkInvoker( DoWork ); worker.BeginInvoke( _initializeCallback, _progressCallback , _primaryStatusTextCallback , _secondaryStatusTextCallback, new AsyncCallback( EndWork ), null ); }
/// <summary> /// Implementation of the async work invoker /// </summary> /// <remarks> /// We're using the delegate BeginInvoke() / EndInvoke() pattern here /// </remarks> protected override void BeginInvokeCore() { WorkInvoker worker = new WorkInvoker(DoWork); worker.BeginInvoke( _initializeCallback, _progressCallback, _primaryStatusTextCallback, _secondaryStatusTextCallback, new AsyncCallback(EndWork), null); }
static void Main() { WorkInvoker method = Work; // Start the delegate; control returned immediately to caller // We also have the callback delegate 'Done' that is automatically called // upon completion. method.BeginInvoke("test", Done, method); // Caller can carry on with other operations in *parallel* here double d = 1.0; Thread.Sleep(5000); Console.WriteLine("Value {0}", d); Console.ReadLine(); }
private void EndWork(IAsyncResult result) { AsyncResult asyncResult = (AsyncResult)result; WorkInvoker asyncDelegate = (WorkInvoker)asyncResult.AsyncDelegate; try { asyncDelegate.EndInvoke(result); OnFinish(EventArgs.Empty); } catch (Exception e) { // Marshal exceptions back to the UI OnError(new ErrorEventArgs(e)); } // catch // { // // Do our exception handling; include a default catch // // block because this is the final handler on the stack for this // // thread, and we need to log these kinds of problems // OnError( new ErrorEventArgs( null ) ); // } }