From 522b59556ddc71eefa305791d6905dc9725dc254 Mon Sep 17 00:00:00 2001 From: Isaac Abraham Date: Sat, 20 Oct 2018 11:46:22 +0200 Subject: [PATCH 1/2] Update async.md Improve Cancellation Token sample --- .../async.md | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/fsharp/tutorials/asynchronous-and-concurrent-programming/async.md b/docs/fsharp/tutorials/asynchronous-and-concurrent-programming/async.md index 16128fb3dbaf0..7b124de5f5a6b 100644 --- a/docs/fsharp/tutorials/asynchronous-and-concurrent-programming/async.md +++ b/docs/fsharp/tutorials/asynchronous-and-concurrent-programming/async.md @@ -185,22 +185,22 @@ In contrast, F# async workflows are more naturally cancellable. Cancellation is Example: ```fsharp -open System -open System.Net +open System.Threading -let uploadDataAsync url data = +let calculateForEver() = async { - let uri = Uri(url) - use webClient = new WebClient() - webClient.UploadStringAsync(uri, data) + while true do + printfn "Working..." + do! Async.Sleep 1000 } -let workflow = uploadDataAsync "https://url-to-upload-to.com" "hello, world!" - +let workflow = calculateForEver() let token = new CancellationTokenSource() -Async.Start (workflow, token) -// Immediately cancel uploadDataAsync after it's been started. +// Start calculateForEver in the background +Async.Start (workflow, token.Token) + +// Executing the next line will stop the workflow token.Cancel() ``` From f5fcfa1517dc29baa7b04f14ac53d8a43c4b626a Mon Sep 17 00:00:00 2001 From: Isaac Abraham Date: Sat, 20 Oct 2018 19:54:31 +0200 Subject: [PATCH 2/2] Update async.md Slight simplification. --- .../async.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/fsharp/tutorials/asynchronous-and-concurrent-programming/async.md b/docs/fsharp/tutorials/asynchronous-and-concurrent-programming/async.md index 7b124de5f5a6b..ee54cd46ce336 100644 --- a/docs/fsharp/tutorials/asynchronous-and-concurrent-programming/async.md +++ b/docs/fsharp/tutorials/asynchronous-and-concurrent-programming/async.md @@ -187,21 +187,21 @@ Example: ```fsharp open System.Threading -let calculateForEver() = +// Create a workflow which will loop forever. +let workflow = async { while true do printfn "Working..." do! Async.Sleep 1000 } + +let tokenSource = new CancellationTokenSource() -let workflow = calculateForEver() -let token = new CancellationTokenSource() - -// Start calculateForEver in the background -Async.Start (workflow, token.Token) +// Start the workflow in the background +Async.Start (workflow, tokenSource.Token) // Executing the next line will stop the workflow -token.Cancel() +tokenSource.Cancel() ``` And that’s it!