应该为 BeginInvoke 的 @object 参数传递什么?
我有一个事件委托,其定义如下:
public delegate void CallbackDelegate(Data data);
public event CallbackDelegate OnDataComplete;
我异步引发事件:
// Raise the OnDataComplete event
OnDataComplete.BeginInvoke(new Data(), null, null);
随后,BeginInvoke
的签名如下所示:
IAsyncResult CallbackDelegate.BeginInvoke(Data data, AsyncCallback callback, object @object)
在我见过的大多数示例中,调用 BeginInvoke
@object
参数为 null
,但我找不到解释该参数用途的文档。
那么该参数的用途是什么?我们可以用它做什么?
I have an event delegate that is defined as follows:
public delegate void CallbackDelegate(Data data);
public event CallbackDelegate OnDataComplete;
I raise the event asynchronously:
// Raise the OnDataComplete event
OnDataComplete.BeginInvoke(new Data(), null, null);
Subsequently, the signature of BeginInvoke
looks like:
IAsyncResult CallbackDelegate.BeginInvoke(Data data, AsyncCallback callback, object @object)
In most examples I've seen BeginInvoke
is called with the @object
parameter being null
, but I can't find the documentation which explains what is the purpose of that parameter.
So what is the purpose of that parameter? What can we use it for?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以在那里提供任何你想要的东西。在 AsyncResult 方法中,您可以使用 IAsyncResult.AsyncState 检索此值。它就在那里供你使用。
You can provide anything you want there. In the AsyncResult method you can retrieve this value with IAsyncResult.AsyncState. It's there for your use.
这样您就可以将任何相关信息从您的方法传递到回调。由于 C# 有 lambda 表达式,并且委托可以有状态,所以有时这是无用的,您可以只传递 null。但它有点类似于
Control.Tag
,它允许您向回调提供可能会方便的信息。更新:
它存在的根源可以追溯到只有函数指针、没有闭包的语言。 (你可能想查找“闭包”这个词……我无法非常简洁地解释它。)在 C 中,只有函数指针,没有委托;因此,函数指针不能保存状态。因此,每当您提供回调时,被调用者都会通过为您传递附加指针来帮助您,以便您可以将数据传递给可能需要的回调。在 .NET 中,这些不太必要(因为委托有
Target
对象并且可以保存状态),但有时它们很方便,这就是它们的来源。It's so that you can pass any relevant information from your method to the callback. Since C# has lambda expressions and since delegates can have state, sometimes this is useless, and you can just pass null. But it's a bit similar to
Control.Tag
, and it lets you give information to the callback that it might find handy.Update:
The origin of why it even exists goes back to languages that only had function pointers, with no closure. (You might want to look up the word "closure"... I can't explain it very concisely.) In C, there's only function pointers and not delegates; hence, function pointers can't hold state. So whenever you provided a callback, the callee helped you by passing an additional pointer for you, so you could pass data to your callback that it might need. In .NET, these are less necessary (because delegates have
Target
objects and can hold state), but sometimes they're handy and that's where they come from.这只是一个状态对象,最终位于 IAsyncResult.AsyncState 可以在 AsyncCallback 代码中检索。有点像ThreadPool.QueueWorkItem(WaitCallback, Object)。
That's just a state object that ends up in IAsyncResult.AsyncState that can be retrieved in your AsyncCallback code. Kinda like the ThreadPool.QueueWorkItem(WaitCallback, Object).