What is the difference between an async delegate and an async method?

What is the difference between an async delegate and an async method?

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

+6
c #
source share
2 answers

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

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

The Invoke () method calls the delegate target synchronously, like a regular call. The BeginInvoke () method is an asynchronous call, the target method is executed in the thread pool thread. An EndInvoke () call is required after the method completes to allocate resources allocated for the call and to re-create any exception that might interrupt the call.

The .NET structure contains many classes that have the BeginXxxx () method. The MSDN library refers to them as asynchronous operations, not asynchronous methods. They trigger an operation that runs asynchronously.

Starting with .NET 4.5 and the supported C # version 5, asynchronous operations, whose name ends in Async and returns the task, can be called in the expression wait . When used in a method that has an async modifier. This greatly simplifies working with asynchronous operations that are important in WinRT, where many common operations are asynchronous.

+6
source share

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

0
source share

All Articles