简体   繁体   中英

TcpClient.ConnectAsync or BeginConnect/EndConnect in F# async workflow

TcpClient.ConnectAsync is a Task, which doesn't work readily with F# async workflows.

I think I'm missing something really simple here -- is there a general way to use either the Async or Begin/End functions from workflows?

Take a look at Async.AwaitTask and Async.FromBeginEnd .

Generally this will work:

async {
  ...
  do! client.ConnectAsync(address, port) |> Async.AwaitTask
}

However, because the Async module only works directly with Task<'T> and ConnectAsync returns Task , the code required in this particular case is slightly longer:

do! client.ConnectAsync(address, port) |> Async.AwaitIAsyncResult |> Async.Ignore

or

let! _ = client.ConnectAsync(address, port) |> Async.AwaitIAsyncResult

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM