同步和异步回调

发布于 2024-08-03 04:36:56 字数 199 浏览 6 评论 0原文

在阅读 MSDN 文档和代码示例时,我对一些术语感到困惑。

C# 中的回调是什么?特别是,什么是同步和异步回调?

请从外行人的角度解释一下这些。

另外,请解释一下 IAsyncCallback IAsyncResult 接口。 我们该如何实施呢? (非常简单的例子)

提前致谢。

I get confused with some terms while reading MSDN documents and code samples.

What are callbacks in C#? In particular, what are synchronous and asynchronous callbacks ?

Please explain these from a layman's point of view.

Also, please explain the IAsyncCallback IAsyncResult interface.
How can we implement it? (with very simple example)

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

转瞬即逝 2024-08-10 04:36:56

IAsyncCallback 接口不存在,因此您无法实现它。

我怀疑您实际上想了解 IAsyncResult 界面

我建议您阅读 MSDN 上的此页面


IAsyncResult 接口 表示一个操作(例如 Web 请求或数据库调用)在后台运行,同时您的代码继续执行。它可以告诉您操作是否完成(IsCompleted 属性)。它还为您提供了一个 WaitHandle 对象< /a> (AsyncWaitHandle 属性)可用于等待操作完成。 (通过调用 result.AsyncWaitHandle.WaitOne()

您可以通过调用 BeginWhatever 获得 IAsyncResult > 方法。 (BeginExecuteReader, BeginGetResponse ,以及许多其他)。 BeginWhatever 方法将采用操作所需的任何参数(例如,BeginExecuteReader 可以采用 CommandBehavior 参数),并且可以采用 AsyncCallback 委托(不是接口)和状态参数。返回一个 IAsyncResult 对象。

AsyncCallback 委托是您提供的一个方法,操作完成时将调用该方法。它通常会在不同的线程上调用,所以要小心。您的 AsyncCallback 方法将获得与 BeginWhatever 方法之前为您提供的相同的 IAsyncResult。 state参数放入IAsyncResult并被系统忽略;您可以在 AsyncCallback 方法中使用它来跟踪操作的用途。 (状态可以是您想要的任何状态,包括 null

在 AsyncCallback(或其他任何地方)内,您可以调用 EndWhatever 方法对应于您首先调用的 BeginWhatever 方法。您必须从 BeginWhatever 或 AsyncCallback 为其提供 IAsyncResult。当您调用它时,它将等待操作完成(如果尚未完成),然后返回操作结果。 (假设操作返回一些内容;例如,WebRequest.EndGetResponse 将返回 WebResponse)。如果操作过程中发生任何错误,EndWhatever将抛出异常。


如果您想创建自己的可以在后台运行的操作,您可以实现 IAsyncResult。您还可以创建 BeginWhateverEndWhatever > 分别返回和获取它的方法。有关实现 IAsyncResult 的详细信息,请参阅此处

The IAsyncCallback interface does not exist, so you cannot implement it.

I suspect that you actually want to know about the IAsyncResult interface.

I recommend that you read this page on MSDN.


The IAsyncResult interface represents an operation (such as a web request or a database call) that is running in the background, while your code continues to execute. It can tell you whether the operation finished (the IsCompleted property). It also gives you a WaitHandle object (the AsyncWaitHandle property) which can be used to wait until the operation finishes. (By calling result.AsyncWaitHandle.WaitOne())

You get an IAsyncResult by calling a BeginWhatever method. (BeginExecuteReader, BeginGetResponse, and many others). The BeginWhatever method will take any parameters needed for the operation (For example, BeginExecuteReader can take a CommandBehavior parameter), and can take an AsyncCallback delegate (not interface) and a state parameter. In returns an IAsyncResult object.

The AsyncCallback delegate is a method that you supply, which will be called when the operation finishes. It will usually be called on a different thread, so be careful in it. Your AsyncCallback method will be given the same IAsyncResult that the BeginWhatever method gave you earlier. The state parameter is put into the IAsyncResult and ignored by the system; you can use it in your AsyncCallback method to keep track of what the operation was for. (The state can be whatever you want it to be, including null)

Inside your AsyncCallback (or anywhere else), you can call the EndWhatever method that corresponds to the BeginWhatever method you called in the first place. You have to give it the IAsyncResult from BeginWhatever or from the AsyncCallback. When you call it, it will wait for the operation to finish (if it isn't already finished), and then give you back the operation's result. (Assuming the operation returns something; for example, WebRequest.EndGetResponse will return a WebResponse). If any error occurred during the operation, EndWhatever will throw an exception.


You would implement IAsyncResult if you want to create your own operation that can run in the background. You would also create BeginWhatever and EndWhatever methods that return and take it, respectively. For more information on implementing IAsyncResult, see here.

我ぃ本無心為│何有愛 2024-08-10 04:36:56

使用同步回调,调用方法(或线程)必须等待被调用方法完成才能继续处理 - 非常类似于“普通”方法调用。

通过异步回调,调用方法(或线程)可以继续处理其他输入或事件或其他任何内容,而无需等待被调用方法完成。

对于应用程序的用户界面线程,如果您不希望它在应用程序执行某些冗长的过程时“冻结”,则需要使用异步回调。

With a synchronous callback the calling method (or thread) has to wait until the called method has completed before it can carry on processing - much like an "ordinary" method call.

With an asynchronous callback the calling method (or thread) can carry on processing other inputs or events or whatever without waiting for the called method to complete.

For an applications User Interface thread if you don't want it to "freeze" while the application is performing some lengthy process you need to use asynchronous callbacks.

独留℉清风醉 2024-08-10 04:36:56

回调只不过是一个委托。使用术语回调是因为您通常将其(委托)传递到方法(作为参数),然后在该方法中调用以发出某些信号。

同步和异步回调就是这样。同步,在调用该方法(启动操作)的同一线程上执行。异步通常在另一个线程上执行(但并非总是如此)

IAsyncCallback 接口是启动异步任务的模板。您传入一个回调(委托),该回调在工作完成时被调用。有一个属性可以确定该方法是否同步执行。

A callback is nothing more than a delegate. The term callback is used because you generally pass it (the delegate) into a method (as an argument) and is then invoked in that method to signal something.

Sync and Async callbacks are that. Synchronous, execute on the same thread that called the method (started the action). Async are generally executed on another thread (but not always)

The IAsyncCallback interface is a template to initiate an asynchronous task. You pass in a callback (delegate) which is invoked when the work is complete. There is a property to determine if the method execute synchronously.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文