Playback starts YouTube’s embedded player (privacy-enhanced mode).
How to Cancel Tasks in C# - Using CancellationTokenSource and CancellationToken
ChannelBrian LagunasDescription from the creator
In this video, I answer the question "How to Cancel Tasks in C# using the CancellationTokenSource and CancellationToken objects?".
Cancelling a Task in C# is pretty straight foreword. There are actually several ways to cancel a Task in C#. The three most common ways to cancel a task are by polling , by registering a callback, and by using the Wait handle. You can even cancel multiple tokens simultaneously.
In this video, we use the polling approach as that is meant for scenarios in which you have long-running computations that loop or recurse.
The general pattern used to cancel a task is:
- Instantiate a CancellationTokenSource object, which manages and sends cancellation notification to the individual cancellation tokens.
- Pass the token returned by the CancellationTokenSource.Token property to each task or thread that listens for cancellation.
- Provide a mechanism for each task or thread to respond to cancellation.
- Call the CancellationTokenSource.Cancel method to provide notification of cancellation.
It's important to note that you should always call the CancellationTokenSource.Dispose method when you have finished using the cancellation token source to free any unmanaged resources it holds.
Be sure to watch my new Pluralsight course "Introduction to Prism for WPF":
pluralsight.pxf.io/bE3rB
Sponsor Me:
github.com/sponsors/brianlagunas
Follow Me:
Twitter: twitter.com/brianlagunas
Twitch: twitch.tv/brianlagunas
Blog: brianlagunas.com
GitHub: github.com/brianlagunas
Cancelling a Task in C# is pretty straight foreword. There are actually several ways to cancel a Task in C#. The three most common ways to cancel a task are by polling , by registering a callback, and by using the Wait handle. You can even cancel multiple tokens simultaneously.
In this video, we use the polling approach as that is meant for scenarios in which you have long-running computations that loop or recurse.
The general pattern used to cancel a task is:
- Instantiate a CancellationTokenSource object, which manages and sends cancellation notification to the individual cancellation tokens.
- Pass the token returned by the CancellationTokenSource.Token property to each task or thread that listens for cancellation.
- Provide a mechanism for each task or thread to respond to cancellation.
- Call the CancellationTokenSource.Cancel method to provide notification of cancellation.
It's important to note that you should always call the CancellationTokenSource.Dispose method when you have finished using the cancellation token source to free any unmanaged resources it holds.
Be sure to watch my new Pluralsight course "Introduction to Prism for WPF":
pluralsight.pxf.io/bE3rB
Sponsor Me:
github.com/sponsors/brianlagunas
Follow Me:
Twitter: twitter.com/brianlagunas
Twitch: twitch.tv/brianlagunas
Blog: brianlagunas.com
GitHub: github.com/brianlagunas
About this tutorial
Brian Lagunas published this 6:23 video tutorial on August 29, 2020. It has been watched 65,309 times on YouTube.
- Channel
- Brian Lagunas
- Category
- Science & Technology
- Duration
- 6:23
- Published
- August 29, 2020
- Views
- 65,309
- Likes
- 1,357
Tags
Share
This tutorial in other languages
- FRComment annuler les tâches en C # - L'utilisation CancellationTokenSource et CancellationToken
- ESCómo cancelar tareas en C # - Uso CancellationTokenSource y CancellationToken
- PTComo cancelar tarefas em C # - Usando CancellationTokenSource e CancellationToken
- ITCome cancellare le attività in C # - Utilizzando CancellationTokenSource e CancellationToken
- DEWie man Abbrechen Aufgaben in C # - Verwenden von CancellationTokenSource und CancellationToken
- NLHoe naar Taken annuleren in C # - Het gebruik van CancellationTokenSource en CancellationToken
- RUКак отменить задачи в C # - Использование CancellationTokenSource и CancellationToken
- PLJak anulować zadania w C # - Korzystanie CancellationTokenSource i CancellationToken
- JAC#でタスクをキャンセルする方法 - CancellationTokenSourceとCancellationTokenを使用して
- ARكيفية الغاء المهام في C # - استخدام CancellationTokenSource وCancellationToken
- HIकार्य रद्द करने के लिए कैसे सी # में - CancellationTokenSource और CancellationToken का उपयोग करना
- IDBagaimana Batal Tugas di C # - Menggunakan CancellationTokenSource dan CancellationToken