当使用超时和 CancellationToken 调用 Async.RunSynchronously 时,超时值似乎被忽略了。我可以通过在 CancellationToken 上调用 CancelAfter 来解决此问题,但理想情况下,我希望能够区分工作流中发生的异常 TimeOutExceptions 和 OperationCanceledExceptions。
我相信下面的示例代码证明了这一点。
open System
open System.Threading
let work =
async {
let endTime = DateTime.UtcNow.AddMilliseconds(100.0)
while DateTime.UtcNow < endTime do
do! Async.Sleep(10)
Console.WriteLine "working..."
raise ( Exception "worked for more than 100 millis" )
}
[<EntryPoint>]
let main argv =
try
Async.RunSynchronously(work, 50)
with
| e -> Console.WriteLine (e.GetType().Name + ": " + e.Message)
let cts = new CancellationTokenSource()
try
Async.RunSynchronously(work, 50, cts.Token)
with
| e -> Console.WriteLine (e.GetType().Name + ": " + e.Message)
cts.CancelAfter(80)
try
Async.RunSynchronously(work, 50, cts.Token)
with
| e -> Console.WriteLine (e.GetType().Name + ": " + e.Message)
Console.ReadKey(true) |> ignore
0
输出如下,表明超时仅在第一种情况下有效(未指定 CancelationToken)
working...
working...
TimeoutException: The operation has timed out.
working...
working...
working...
working...
working...
working...
working...
Exception: worked for more than 100 millis
working...
working...
working...
working...
working...
working...
OperationCanceledException: The operation was canceled.
这是预期的行为吗?有没有办法得到我所追求的行为?
谢谢!