如何Async.AwaitTask在普通任务(任务不是LT&; T>)? [英] How to Async.AwaitTask on plain Task (not Task<T>)?

查看:144
本文介绍了如何Async.AwaitTask在普通任务(任务不是LT&; T>)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图消耗F#一个C#库。该库大量使用异步电动机/等待。我想在F#中的异步{...} 工作流程中使用。

我看我们可以通过 Async.AwaitTask 的异步C#方法返回任务< T> ,但对于那些返回平原工作

或许,有没有帮手,这些转换为异步<部> 任务转换为任务<部> 所以它会与 Async.AwaitTask

解决方案

您可以使用ContinueWith:

 让awaitTask(T:任务)= t.ContinueWith(乐趣笔 - >())|> Async.AwaitTask

或者AwaitIAsyncResult与无限超时:

 让awaitTask(T:任务)= T |> Async.AwaitIAsyncResult |> Async.Ignore

I'm trying to consume a C# library in F#. The library makes heavy use of async/await. I want to use within an async { ... } workflow in F#.

I see we can Async.AwaitTask on async C# methods returning Task<T>, but what about those returning plain Task?

Perhaps, is there a helper to convert these to Async<unit> or to convert Task to Task<unit> so it will work with Async.AwaitTask?

解决方案

You can use ContinueWith:

let awaitTask (t: Task) = t.ContinueWith (fun t -> ()) |> Async.AwaitTask

Or AwaitIAsyncResult with infinite timeout:

let awaitTask (t: Task) = t |> Async.AwaitIAsyncResult |> Async.Ignore

这篇关于如何Async.AwaitTask在普通任务(任务不是LT&; T&GT;)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆