如何为TcpListener编写我自己的异步包装?
问题描述:
对于 TcpListener.BeginAcceptTcpClient()
和EndAcceptTcpClient()
都没有F#异步包装(在PowerPack中)。如何为TcpListener编写我自己的异步包装?
我该如何编写自己的包装,以便我可以使用let!
和async
关键字并行运行?
答
你检查了Async.BuildPrimitive
函数吗?我认为你可以这样做:
type TcpListener with
member x.AsyncAcceptClient() =
Async.BuildPrimitive(x.BeginAcceptTcpClient, x.EndAcceptTcpClient)
创建一个扩展方法返回适当的异步结果。
谢谢,正是我在寻找的 – esac 2009-09-23 17:02:21
在最近的F#版本中,它被称为Async.FromBeginEnd。 – Govert 2011-09-21 19:03:16