3

I know that in C# (and VB.Net) I can create a delegate, point this at a method and then use this delegate to run my method either synchronously or asynchronously. I know how to do this.

Sometimes I use a delegate to run a method asynchronously, and sometimes these methods have input parameters. To run my method asynchronously I call the delegates BeginInvoke method, providing my input parameters, followed by "null, null".

I supply "null, null" as the final two parameters although Visual Studio's intellisense is suggesting I instead supply an AsyncCallback object named callback followed by a second object simply named @object as shown below.

Picture

What is an AsyncCallback and how is it used, and what is the purpose of the other object named @object suggested by intellisense?

Thanks

JMK
  • 27,273
  • 52
  • 163
  • 280
  • possible duplicate of [What is AsyncCallback?](http://stackoverflow.com/questions/1047662/what-is-asynccallback) – phoog Apr 20 '12 at 14:09
  • @phoog I see your point, however I can't find any explanation for the purpose of the second object named `@object` which is a part of my question! – JMK Apr 20 '12 at 14:29
  • in the accepted answer to that question: "In addition, we are specifying some object that we might need as the state of the call. For this example, we are sending the stream object in because we will need to call EndRead and close the stream." – phoog Apr 20 '12 at 14:31

2 Answers2

7

AsyncCallback is a delegate type that acts as a callback function.

If you pass a function as this parameter, .Net will call your callback after the delegate finishes executing asynchronously.

The second parameter allows you to pass state to the AsyncCallback so that your callback function can know what it's coming from.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

Basically AsyncCallback is used to process a result of async operation. And state is used to be able to pass an object between async and non async environments (async and non async functions)

A good example is Using an AsyncCallback Delegate to End an Asynchronous Operation, where async call is made to get Dns information (so undefined term operation ecxecution and handling)

Hope this helps.

JMK
  • 27,273
  • 52
  • 163
  • 280
Tigran
  • 61,654
  • 8
  • 86
  • 123