【发布时间】:2014-06-19 17:01:35
【问题描述】:
.NET 中有很多类使用旧的 Asynchronous Programming Model (APM),“不再推荐用于新开发”。 APM 使用 Begin/End 方法对,End 方法将IAsyncResult 对象作为参数。一个这样的类是TcpClient,您可以使用它进行异步连接,如下所示:
private void SomeMethod()
{
this.tcpClient = new TcpClient();
IAsyncResult result = this.tcpClient.BeginConnect(ip, port, EndConnect, null);
}
private void EndConnect(IAsyncResult asyncResult)
{
this.tcpClient.EndConnect(asyncResult);
// ... do stuff ...
}
Task-based Asynchronous Pattern (TAP) 是一种更现代的异步编程形式,可通过使用 async 和 await 关键字来实现。
如果你有一个像 TcpClient 这样的类,它使用 APM 模型并且不公开任何任务,那么如何将其异步方法调整到 TAP 以便它们可以与async/await 一起使用?
【问题讨论】:
标签: c# asynchronous task async-await