let runWithTimeout (timeout: int) (action: unit -> 'a) : 'a option =
let cts = new CancellationTokenSource(timeout)
try
let task = async { let! res = Task.Run(action, cts.Token)
return res }
Some(Async.RunSynchronously(task))
with :? OperationCanceledException -> None
But for Task.Run(action, cts.Token)
:
Type constraint mismatch. The type 'Task<'a>' is not compatible with type 'Async<'b>'