C# Tutorial – How to use Cancellation Tokens. Manage Long-Running Tasks in your Code Efficiently!



C# Tutorial – How to use Cancellation Tokens. Manage Long-Running Tasks in your Code Efficiently!

C# Tutorial - How to use Cancellation Tokens. Manage Long-Running Tasks in your Code Efficiently!

#dotnet #csharp #.NET #softwaredevelopment

Ongoing tasks in C# need at some point to be cancelled. Whether it’s something that happens on a background task or on a thread, being able to gracefully exist a task is essential.

Cancellation tokens provide us with a point of reference which can be checked by tasks to decide whether they should stop running.

In this video I show you two examples that use cancellation tokens. The first one is an example of when Cancellation Tokens are overkill, using a WinForms app and a timer. The second example is a more relevant use case, demonstrating the use of cancellation tokens to gracefully halt an ongoing thread which was queued onto the thread pool.