Tutorial :What's the different between an async delegate and async method?



Question:

What's the different between an async delegate and async method?

Someone told me they were different in C#, but I thought they were the same thing.


Solution:1

Delegates first. When you declare one, the compiler automatically generates three methods for the delegate type:

  • Invoke(...), taking the same arguments as the delegate declaration
  • BeginInvoke(..., AsyncCallback, object) where ... are the declared arguments
  • EndInvoke(IAsyncResult)

The Invoke() method calls the delegate target synchronously, just like a plain call. The BeginInvoke() method is the asynchronous call, the target method runs on a thread-pool thread. The EndInvoke() call is required after the method completes to release resources allocated for the call and to re-raise any exception that might have aborted the call.

The .NET framework contains many classes that have a BeginXxxx() method. The MSDN Library refers to them as asynchronous operations, not asynchronous methods. They start an operation that completes asynchronously.

Starting with .NET 4.5 and supported by C# version 5, the asynchronous operations whose name end in Async and return a Task can be called in an await expression. When used in a method that has the async modifier. This greatly simplifies dealing with asynchronous operations, important in WinRT where many common operations are asynchronous.


Solution:2

For the differences, as well as some further discussion, see Asynchronous methods and asynchronous delegates right here on SO.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »