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

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

问题描述

我正在尝试在 F# 中使用 C# 库.该库大量使用 async/await.我想在 F# 中的 async { ... } 工作流中使用.

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#.

我看到我们可以在异步 C# 方法上 Async.AwaitTask 返回 Task,但是那些返回纯 Task 的方法呢?

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

也许,是否有帮助程序将这些转换为 Async 或将 Task 转换为 Task 所以它会使用 Async.AwaitTask?

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?

推荐答案

可以使用ContinueWith:

You can use ContinueWith:

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

或无限超时的 AwaitIAsyncResult:

Or AwaitIAsyncResult with infinite timeout:

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

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

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