简体   繁体   English

关于.Net中的委托,`AsyncCallback`和`@ object`的目的是什么?

[英]What is the purpose of `AsyncCallback` and `@object` in regards to delegates in .Net

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. 我知道在C#(和VB.Net)中我可以创建一个委托,指向一个方法,然后使用这个委托同步或异步地运行我的方法。 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". 要异步运行我的方法,我调用委托BeginInvoke方法,提供我的输入参数,然后是“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. 我提供“null,null”作为最后两个参数,尽管Visual Studio的intellisense建议我提供一个名为callback的AsyncCallback对象,然后是第二个简单命名为@object ,如下所示。

图片

What is an AsyncCallback and how is it used, and what is the purpose of the other object named @object suggested by intellisense? 什么是AsyncCallback以及它是如何使用的,以及intellisense建议的另一个名为@object对象的目的是什么?

Thanks 谢谢

AsyncCallback is a delegate type that acts as a callback function. AsyncCallback是一个委托类型,充当回调函数。

If you pass a function as this parameter, .Net will call your callback after the delegate finishes executing asynchronously. 如果将函数作为此参数传递,则.Net将在委托异步执行完成后调用您的回调。

The second parameter allows you to pass state to the AsyncCallback so that your callback function can know what it's coming from. 第二个参数允许您将状态传递给AsyncCallback,以便您的回调函数可以知道它的来源。

Basically AsyncCallback is used to process a result of async operation. 基本上AsyncCallback用于处理异步操作的结果。 And state is used to be able to pass an object between async and non async environments (async and non async functions) 并且state用于能够在异步和非异步环境(异步和非异步函数)之间传递对象

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) 一个很好的例子是使用AsyncCallback委托来结束异步操作 ,其中进行异步调用以获取Dns信息(因此未定义的术语操作执行和处理)

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM