同步和异步回调
在阅读 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
IAsyncCallback
接口不存在,因此您无法实现它。我怀疑您实际上想了解
IAsyncResult
界面。我建议您阅读 MSDN 上的此页面。
IAsyncResult
接口 表示一个操作(例如 Web 请求或数据库调用)在后台运行,同时您的代码继续执行。它可以告诉您操作是否完成(IsCompleted
属性)。它还为您提供了一个WaitHandle
对象< /a> (AsyncWaitHandle
属性)可用于等待操作完成。 (通过调用result.AsyncWaitHandle.WaitOne()
)您可以通过调用
Begin
Whatever
获得IAsyncResult
> 方法。 (BeginExecuteReader
,BeginGetResponse
,以及许多其他)。Begin
Whatever
方法将采用操作所需的任何参数(例如,BeginExecuteReader
可以采用CommandBehavior
参数),并且可以采用 AsyncCallback 委托(不是接口)和状态参数。返回一个IAsyncResult
对象。AsyncCallback 委托是您提供的一个方法,操作完成时将调用该方法。它通常会在不同的线程上调用,所以要小心。您的 AsyncCallback 方法将获得与
Begin
Whatever
方法之前为您提供的相同的 IAsyncResult。 state参数放入IAsyncResult并被系统忽略;您可以在 AsyncCallback 方法中使用它来跟踪操作的用途。 (状态可以是您想要的任何状态,包括null
)在 AsyncCallback(或其他任何地方)内,您可以调用
End
Whatever
方法对应于您首先调用的Begin
Whatever
方法。您必须从Begin
Whatever
或 AsyncCallback 为其提供 IAsyncResult。当您调用它时,它将等待操作完成(如果尚未完成),然后返回操作结果。 (假设操作返回一些内容;例如,WebRequest.EndGetResponse
将返回WebResponse
)。如果操作过程中发生任何错误,End
Whatever
将抛出异常。如果您想创建自己的可以在后台运行的操作,您可以实现 IAsyncResult。您还可以创建
Begin
Whatever
和End
Whatever
> 分别返回和获取它的方法。有关实现 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 (theIsCompleted
property). It also gives you aWaitHandle
object (theAsyncWaitHandle
property) which can be used to wait until the operation finishes. (By callingresult.AsyncWaitHandle.WaitOne()
)You get an
IAsyncResult
by calling aBegin
Whatever
method. (BeginExecuteReader
,BeginGetResponse
, and many others). TheBegin
Whatever
method will take any parameters needed for the operation (For example,BeginExecuteReader
can take aCommandBehavior
parameter), and can take an AsyncCallback delegate (not interface) and a state parameter. In returns anIAsyncResult
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
Begin
Whatever
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, includingnull
)Inside your AsyncCallback (or anywhere else), you can call the
End
Whatever
method that corresponds to theBegin
Whatever
method you called in the first place. You have to give it the IAsyncResult fromBegin
Whatever
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 aWebResponse
). If any error occurred during the operation,End
Whatever
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
Begin
Whatever
andEnd
Whatever
methods that return and take it, respectively. For more information on implementing IAsyncResult, see here.使用同步回调,调用方法(或线程)必须等待被调用方法完成才能继续处理 - 非常类似于“普通”方法调用。
通过异步回调,调用方法(或线程)可以继续处理其他输入或事件或其他任何内容,而无需等待被调用方法完成。
对于应用程序的用户界面线程,如果您不希望它在应用程序执行某些冗长的过程时“冻结”,则需要使用异步回调。
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.
回调只不过是一个委托。使用术语回调是因为您通常将其(委托)传递到方法(作为参数),然后在该方法中调用以发出某些信号。
同步和异步回调就是这样。同步,在调用该方法(启动操作)的同一线程上执行。异步通常在另一个线程上执行(但并非总是如此)
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.