Posts

Showing posts with the label await

How to stop C# async methods

Image
Scenario: a WPF application which when the user presses a button goes off and runs something asynchronously , so that the user is still able to interact with the user interface without it freezing. Not only that, I want to be able to stop the asynchronous operation on receipt of another button press so that it exits graciously. The is where CancellationToken structure comes in, that is used to propagates notifications that operations should be cancelled. Some useful StackOverflow resources on this subject can be found here: https://stackoverflow.com/questions/15614991/simply-stop-an-async-method https://stackoverflow.com/questions/7343211/cancelling-a-task-is-throwing-an-exception Some instructions to implement this in a WPF / MVVM kind of environment are given: Step 1: Create a WPF application Step 2: Include event-handling classes Specifically this means classes to implement ICommand RelayCommand.cs [code language="csharp"] using System; using Sy...