Questions tagged [cancellationtokensource]

Provides a cancellation token in .NET for cooperative cancellation of asynchronous or long-running synchronous operations.

Provides a cancellation token in NET. Starting with .NET Framework 4.0, the .NET Framework uses a unified model for cooperative cancellation of asynchronous or long-running synchronous operations that involves two objects:

  • A CancellationTokenSource object, which provides a cancellation token through its Token property and sends a cancellation message by calling its Cancel or CancelAfter method.
  • A CancellationToken object, which indicates whether cancellation is requested.

A CancellationToken enables cooperative cancellation between threads, thread pool work items, or Task objects. You create a cancellation token by instantiating a CancellationTokenSource object, which manages cancellation tokens retrieved from its CancellationTokenSource.Token property. You then pass the cancellation token to any number of threads, tasks, or operations that should receive notice of cancellation. The token cannot be used to initiate cancellation. When the owning object calls CancellationTokenSource.Cancel, the IsCancellationRequested property on every copy of the cancellation token is set to true. The objects that receive the notification can respond in whatever manner is appropriate.

References

359 questions
-1
votes
3 answers

Will having a delay between canceling and disposing a CancellationTokenSource provide any assurance that IsCancellationRequested will be set to true?

C# Windows UWP project I have implemented CancellationTokenSource and CancellationToken in an async method that calls another method. This method contains a while loop which maintains the value of a bool variable as true until the token source is…
-1
votes
2 answers

How to cancel a specific task by its ID

I have an WPF application in which I am generating numbers of task for parallel processing. Now when my condition becomes true then I need to cancel particular that task. When I am applying CancellationTokenSource for cancel task then it's cancel…
-1
votes
1 answer

Low performance on TaskCanceledException

I need to run many (10-100k) convertation operations in parallel. In this case I use tasks, SymaphoreSlim to control degree of parallelism and CancellationTokenSource to prevent not running tasks to run when exception is thrown. I'm trying to do…
-1
votes
1 answer

asp.net C# how to do Response.Redirect inside task

Here is my code. protected void Send(object sender, ImageClickEventArgs e) { try { ObjCancellationTokenSource = new CancellationTokenSource(); CancellationToken token = ObjCancellationTokenSource.Token; …
saravanan049
  • 60
  • 10
-1
votes
3 answers

Canceling async method does not work until task is completed

I have an async method. I do not have access to the Frame.Execute() code method. Issue: myTask would not cancel until the Frame.Execute complete execution. I need to immediately cancel Frame.Execute() and myTask when a cancellation…
Rick
  • 55
  • 1
  • 12
-1
votes
2 answers

c# cancel big operation with CancellationTokenSource

I have wpf application that run some math operations . When Start button click i calculate some math, and return value to Gui . every operation has some loop inside. I want give the user Cancel click that stop all the calculate immediately. I saw …
-1
votes
1 answer

Task status not changing to Cancelled when in the middle of execution

I was reading a book on Parallel Programming and I coded the following scenario which worked as expected (Task.Status == TaskStatus.Canceled): It’s possible for a task to end in TaskStatus.Canceled. For this to occur, you must pass a…
Sadiq
  • 786
  • 1
  • 10
  • 35
-1
votes
1 answer

CancellationTokens in parallel threads

I am posting this partly out of intrest on how the Task Parallel Library works, and for spreading knowledge. And also for investigating whether my "Cancellation" updates is the reason to a new issue where the user is suddenly logged out. The project…
-1
votes
2 answers

How to correct cancel Task with Event Handler

Sorry for bad english. In my project i have two Task. One for decoding data from IP camera and fire frame ready event to UI. And other to watch the camera is not offline. If the camera is offline, it throw Error event handler to UI thread. UI…
-1
votes
3 answers

Cancelling a Long Running Task Delay

I am trying to cancel a async delay task (Task.Delay) that was created by a web api request, with an another web api request that issues cancel. It does not look like it cancelled the timer for task.delay. Here is the code snippet that I am trying…
Amzath
  • 3,159
  • 10
  • 31
  • 43
-1
votes
1 answer

how to cancel only parent tasks when cancelling child tasks?

I'we got an async controller action with cancellation token as a parameter: public async Task Index(string NomenCode = "", string ProducerName = "", bool? WithAnalog = null, long? OrderId = null, CancellationToken cancelToken =…
-2
votes
1 answer

How to cancel task but let her to finish the chunk?

I have a program (.NET Core C#) that delete data from DB, for each account - deletes all the data related to the account. I want to add an option to stop the program but finish the deletion for the person the program is on and then stop and exit. I…
-2
votes
1 answer

Cancellation throwing unhandled exception in .Net

This appears to be a common problem but I am unable to find a solution yet. I have checked this in SO Cancelling a Task is throwing an exception My caller: Private Async Sub btnTestTimer_Click(sender As Object, e As EventArgs) Handles…
-2
votes
1 answer

Create Class With Value Which Resets using Task.Delay()

I have some flags values in an C# windows service which I want to temporarily be able to change but that will automatically revert to some default value after a period of time. The bare bones of the class look something like this public class…
Xedni
  • 3,662
  • 2
  • 16
  • 27
1 2 3
23
24