取消执行阻止同步工作的异步工作流程

时间:2016-01-26 11:54:44

标签: .net f#

我不确定是否可以这样做,但我有一个执行阻止操作的异步工作流程。如果它已经运行了很长一段时间,我想取消它,包括当前正在执行工作流程的线程。

这是我的测试用例:

let counter = ref 0
let work = async {
    let computation = async {
        System.Threading.Interlocked.Increment(counter) |> ignore
        let blockingWork = Async.Sleep(2000) |> Async.RunSynchronously
        System.Threading.Interlocked.Increment(counter) |> ignore
    }
    do! computation
}

use cts = new System.Threading.CancellationTokenSource(1000)
let tryCancelled = Async.TryCancelled(work, fun _ -> printf "Cancelled")
do Async.Start(tryCancelled, cts.Token) 
System.Threading.Thread.Sleep(3000) //Simulate other work
counter.contents |> should equal 1

问题是工作流程被成功取消并且调用了补偿功能,但计算继续并递增计数器......

1 个答案:

答案 0 :(得分:1)

使用do执行异步上下文中的blockingWork!阻止工作

let counter = ref 0
let work = async {
    let computation = async {
        System.Threading.Interlocked.Increment(counter) |> ignore
        let blockingWork = Async.Sleep(2000) 
        do! blockingWork 
        System.Threading.Interlocked.Increment(counter) |> ignore
    }
    do! computation
}

let doit() =
    use cts = new System.Threading.CancellationTokenSource(1000)
    let tryCancelled = Async.TryCancelled(work, fun _ -> printf "Cancelled")
    do Async.Start(tryCancelled, cts.Token) 
    System.Threading.Thread.Sleep(3000) //Simulate other work
    counter.contents // |> should equal 1

doit()