什么是正确的实现IAsyncResult接口的? [英] What is a proper implementation of the IAsyncResult interface?
问题描述
我寻找到增加一些灵活性,我已经创建了它建立到远程主机的连接的类,然后进行信息交换(握手)。目前的实现提供了一个连接功能,建立连接,然后阻止等待一个ManualResetEvent的,直到双方已经完成了握手。
I'm looking into adding some flexibility to a class that I've created which establishes a connection to a remote host and then performs an exchange of information (a handshake). The current implementation provides a Connect function which establishes the connection and then blocks waiting on a ManualResetEvent untill the two parties have completed the handshake.
下面是一个什么样叫我的类看起来像一个例子:
Here's an example of what calling my class looks like:
// create a new client instance
ClientClass cc = new ClientClass("address of host");
bool success = cc.Connect(); // will block here until the
// handshake is complete
if(success)
{
}
......这里的的类在内部做什么的过于简单化的高级视图:
..and here's an oversimplified high-level view of what the class does internally:
class ClientClass
{
string _hostAddress;
ManualResetEvent _hanshakeCompleted;
bool _connectionSuccess;
public ClientClass(string hostAddress)
{
_hostAddress = hostAddress;
}
public bool Connect()
{
_hanshakeCompleted = new ManualResetEvent(false);
_connectionSuccess = false;
// start an asynchronous operation to connect
// ...
// ...
// then wait here for the connection and
// then handshake to complete
_hanshakeCompleted.WaitOne();
// the _connectionStatus will be TRUE only if the
// connection and handshake were successful
return _connectionSuccess;
}
// ... other internal private methods here
// which handle the handshaking and which call
// HandshakeComplete at the end
private void HandshakeComplete()
{
_connectionSuccess = true;
_hanshakeCompleted.Set();
}
}
我在寻找进入实施 .NET经典的异步模式这个类。在此过程中,我将提供BeginConnect和EndConnect功能,并允许类的用户编写code是这样的:
I'm looking into implementing the .NET Classic Async Pattern for this class. In doing so, I would provide BeginConnect and EndConnect functions, and allow the users of the class to write code like this:
ClientClass cc = new ClientClass("address of host");
cc.BeginConnect(new AsyncCallback(ConnectCompleted), cc);
// continue without blocking to this line
// ..
void ConnectCompleted(IAsyncResult ar)
{
ClientClass cc = ar.AyncState as ClientClass;
try{
bool success = cc.EndConnect(ar);
if(success)
{
// do more stuff with the
// connected Client Class object
}
}
catch{
}
}
为了能够提供该API我需要创建实现IAsyncResult接口由BeginConnect函数返回一个类,并且分别被传递到EndConnect功能
In order to be able to provide this API I need to create a class that implements the IAsyncResult interface to be returned by the BeginConnect function, and to be passed into the EndConnect function respectively.
现在,我的问题是:什么是实现一类IAsyncResult接口有道
一个显而易见的解决方案是创建一个委托具有匹配签名的连接功能,然后异步调用使用的BeginInvoke该委托 - EndInvoke会,但不是我要找的(这不是很有效)。
One obvious solution would be to create a delegate with a matching signature for the Connect function and then invoke that delegate asynchronously using BeginInvoke - EndInvoke but that is not what I'm looking for (it's not very efficient).
我有我怎么能做到这一点粗略的想法,但偷看了.NET框架内他们是如何实现这个模式在一些地方后,我觉得这将是明智的询问,看看是否有人有这个成功,如果是这样做的有什么问题地区要特别注意。
I have a rough idea of how I could do it but after peeking inside the .NET framework at how they implement this pattern in some places I felt it would be wise to ask and see if anybody has done this successfully and if so what are the problem areas to pay special attention to.
谢谢!
推荐答案
- 杰弗里里氏(MSDN杂志)
- 微软WCF样品
- <一个href="http://blogs.msdn.com/b/nikos/archive/2011/03/14/how-to-implement-iasyncresult-in-another-way.aspx"相对=nofollow>尼科Schuessler (MS员工)
- 在谷歌IAsyncResult的执行情况的详细...
- Jeffery Richter (MSDN Magazine)
- Microsoft's WCF Samples
- Niko Schuessler (MS employee)
- Google "IAsyncResult Implementation" for more...
您也有很多实现的BCL(如 System.Runtime.Remoting.Messaging.AsyncResult
) - 使用反射器或引用来源检查出来
You also have many implementations in the BCL (e.g. System.Runtime.Remoting.Messaging.AsyncResult
) - use reflector or the reference source to check them out.
这篇关于什么是正确的实现IAsyncResult接口的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!