Monday, July 14, 2014

Asycnhronous Delegates

  1. ThreadPool.QueueUserWorkItem does not provide an easy way to pass more than one typed  parameter and receive the result back from a thread after it has finished executing. 
  2.  Asynchronous delegate solves this limitation, allowing any number of typed arguments to be passed in both directions. 
  3. Un-handled exceptions on asynchronous delegates are conveniently rethrown on the original thread
  4. BeginInvoke returns immediately to the caller without waiting for the asynchronous call to complete. You can perform other activities in parallel while the pooled thread is working. When you need the results, simply call EndInvoke on the delegate, passing in the saved IAsyncResult object.
  5. When we call EndInvoke, it waits for the asynchronous delegate to finish executing. It receives the return value (as well as any ref or out parameters). It throws any unhandled exception back to the calling thread.
  6. If you need to call some method after asynchronous method has completed, then you can pass the callback handler method, which accepts IAsyncResult object, while calling BeginInvoke method. This allows the instigating thread to “forget” about the asynchronous delegate, but it requires a bit of extra work at the callback end as you can see in the given example.
  7. The final argument to BeginInvoke is a user state object that populates the AsyncState property ofIAsyncResult. It can contain anything you like; in this case, we’re using it to pass the method delegate to the completion callback, so we can call EndInvoke on i


  • Code References

    1. http://www.codeproject.com/Articles/729574/Thread-Pooling-in-Csharp-Asynchronous-Delegates

    No comments:

    Post a Comment