각 비동기 하위 블록을 각 루프에서 실행하는 비동기 루프가있는 비동기 워크 플로를 만들려고합니다. 그리고이 비동기 하위 블록을 취소 할 수있게하려고합니다. 그러나 취소 할 때 을 입력하지 마십시오. 메인 루프가 취소되기를 원합니다. do! subBlock
다음 줄에서 계속하기를 원합니다.F # 비동기 워크 플로의 하위 블록 취소
도 허용 서명이 내가 Async
에서 볼 수있는 유일한 방법 Async.StartAsTask
입니다 (CancellationToken
소요는 async
로 변환 할 수있는 무언가를 반환),하지만이 취소 할 때 중지 것 같다; 아래에서 "취소됨"을 인쇄 한 다음 다른 것은 표시하지 않습니다.
open System
open System.Threading
open System.Threading.Tasks
// runs until cancelled
let subBlock =
async {
try
while true do
printfn "doing it"
do! Async.Sleep 1000
printfn "did it"
finally
printfn "cancelled!"
}
[<EntryPoint>]
let main argv =
let ctsRef = ref <| new CancellationTokenSource()
let mainBlock =
//calls subBlock in a loop
async {
while true do
ctsRef := new CancellationTokenSource()
do! Async.StartAsTask(subBlock, TaskCreationOptions.None, (!ctsRef).Token)
|> Async.AwaitTask
printfn "restarting"
}
Async.Start mainBlock
//loop to cancel CTS at each keypress
while true do
Console.ReadLine() |> ignore
(!ctsRef).Cancel()
0
어떤 방법이 있습니까?