我可以明确地检查取消/终止异步计算? [英] Can I explicitly check for cancellation / terminate async computation?

查看:134
本文介绍了我可以明确地检查取消/终止异步计算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个异步计算像下面(见内嵌注释):

I have an async computation like the following (see inline comments):

async {
  //...
  do! Async.Sleep(100) //cancellation may happen during sleep
  //... but isn't checked at the end of the sleep, so regular, non-async computations are executed here 
}

为了强制取消支票/常规计算部分之前终止整个异步计算达到,我插入一个有效的无操作做的! Async.Sleep(1)后,立即做的! Async.Sleep(100)。是否有一个更清洁的方式做到这一点?甚至可能像做的! Async.Nop

In order to force a cancellation check / terminate the entire async computation before the "regular" computation part is reached, I insert an effective no-op do! Async.Sleep(1) immediately after the do! Async.Sleep(100). Is there a cleaner way to do this? Possibly even something like do! Async.Nop.

推荐答案

如何是这样的:

let nop = async.Return ()

然后,你可以使用它像:

Then you could use it like:

async {
    // ...
    do! Async.Sleep 100
    do! nop
}

这篇关于我可以明确地检查取消/终止异步计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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