什么是IAsyncResult接口的正确实现? [英] What is a proper implementation of the IAsyncResult interface?
问题描述
我正在考虑为我创建的一个类添加一些灵活性,该类与远程主机建立连接,然后执行信息交换(握手)。目前的实现提供了一个Connect函数,它建立连接,然后阻止等待一个ManualResetEvent,直到双方完成了握手。
这是一个打电话给我的课程的例子看起来像:
//创建一个新的客户端实例
/ pre>
ClientClass cc = new ClientClass(host of host) ;
bool success = cc.Connect(); //将阻止这里直到
//握手完成
如果(成功)
{
}
..这是一个过于简单的高级视图,内容是什么课程:
class ClientClass
{
string _hostAddress;
ManualResetEvent _hanshakeCompleted;
bool _connectionSuccess;
public ClientClass(string hostAddress)
{
_hostAddress = hostAddress;
}
public bool Connect()
{
_hanshakeCompleted = new ManualResetEvent(false);
_connectionSuccess = false;
//开始一个异步操作来连接
// ...
// ...
//等待这里连接和
//然后握手完成
_hanshakeCompleted.WaitOne();
//如果
//连接和握手成功,_connectionStatus将为TRUE
return _connectionSuccess;
}
// ...其他内部私有方法
//处理握手和哪个调用
// HandshakeComplete结束
private void HandshakeComplete()
{
_connectionSuccess = true;
_hanshakeCompleted.Set();
}
}
我正在研究实现 .NET Classic Async Pattern 。在这样做的时候,我将提供BeginConnect和EndConnect函数,并允许该类的用户编写如下代码:
ClientClass cc = new ClientClass(host of host);
cc.BeginConnect(new AsyncCallback(ConnectCompleted),cc);
//继续而不阻止此行
// ..
void ConnectCompleted(IAsyncResult ar)
{
ClientClass cc = ar.AyncState as ClientClass;
try {
bool success = cc.EndConnect(ar);
if(success)
{
//使用
//连接的客户端类对象
}
}
catch {
}
}
为了能够提供这个API,我需要创建一个类,实现由BeginConnect函数返回的IAsyncResult接口,并分别传递给EndConnect函数。
现在,我的问题是:在类中实现IAsyncResult接口的正确方法是什么?
一个明显的解决方案是创建一个具有匹配签名的代理函数,然后使用BeginInvoke - EndInvoke异步调用该委托,但这不是我正在寻找(这不是很有效)。
我有一个粗略的想法如何我可以做到这一点,但是在.NET框架中看到它们如何在某些我觉得它的地方实现这种模式请问有没有人能够成功地做到这一点,如果有的话,请问要特别注意什么问题。
谢谢!
解决方案
- Jeffery Richter (MSDN Magazine)
- Microsoft的WCF示例
- Niko Schuessler (MS员工)
- Google的IAsyncResult实现 ...
您还在BCL中有很多实现(例如 System.Runtime.Remoting.Messaging.AsyncResult
) - 使用反射镜或参考源来检查它们。
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();
}
}
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{
}
}
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.
Now, my question is: What is a proper way to implement the IAsyncResult interface in a class?
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).
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.
Thanks!
- Jeffery Richter (MSDN Magazine)
- Microsoft's WCF Samples
- Niko Schuessler (MS employee)
- Google "IAsyncResult Implementation" for more...
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屋!