Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -185,24 +185,23 @@ 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 =
// Create a workflow which will loop forever.
let workflow =
async {
let uri = Uri(url)
use webClient = new WebClient()
webClient.UploadStringAsync(uri, data)
while true do
printfn "Working..."
do! Async.Sleep 1000
}

let tokenSource = new CancellationTokenSource()

let workflow = uploadDataAsync "https://url-to-upload-to.com" "hello, world!"

let token = new CancellationTokenSource()
Async.Start (workflow, token.Token)
// Start the workflow in the background
Async.Start (workflow, tokenSource.Token)

// Immediately cancel uploadDataAsync after it's been started.
token.Cancel()
// Executing the next line will stop the workflow
tokenSource.Cancel()
```

And that’s it!
Expand Down