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
0
votes
1 answer

How to cancel on going transaction(in controller) from UI in Asp.Net MVC?

I am sending Transaction request from UI to controller and processing that request in controller. This process may consume time around 10 seconds. While processing transaction the user has a provision to cancel the transaction. So i need to check…
ManirajSS
  • 2,295
  • 5
  • 26
  • 50
0
votes
1 answer

F# handling Task cancellation

I am struggling to understand why some code is never executed. Consider this extension method: type WebSocketListener with member x.AsyncAcceptWebSocket = async { try let! client = Async.AwaitTask <| x.AcceptWebSocketAsync…
vtortola
  • 34,709
  • 29
  • 161
  • 263
0
votes
1 answer

Task.RunSynchronously() doesn’t care about CancellationToken

I want to execute a 3rd party function on the current thread (to avoid overhead) but want to be able to use a CancellationToken to interrupt the function at any time. That's how I create and run the task: var task = new Task(proc, cts.Token); // cts…
0
votes
1 answer

How to stop async task and start it again upon user cancellation

I got a requirement to implement a long running process in asynchronously. I have done the implementation using Task.Run() everything worked without any issues. However the requirement got changes and now they want 'cancel' feature of any ongoing…
RSF
  • 510
  • 6
  • 18
0
votes
2 answers

CancellationTokenSource.Cancel not working as expect in WPF

Trying to learn Task, Wait and Cancellation. Started with what I thought was a simple sample from MSDN. Task.Wait Method (CancellationToken) As a console application this runs as expected. That same code does not run as expected in WPF. In WPF t2…
paparazzo
  • 44,497
  • 23
  • 105
  • 176
0
votes
1 answer

Cancellation token and thread not working

I want to cancel a thread and and run another one just after. Here is my code: private void ResetMedia(object sender, RoutedEventArgs e) { cancelWaveForm.Cancel(); // cancel the running thread …
0
votes
0 answers

Cancellation of a long running task in C#

I am working on a project where we have lots of separate components are running and my agenda is to cancel the component process in between based on user input. For example: I have 6 windows service that do all the work and 1 another windows service…
Abhash786
  • 881
  • 2
  • 24
  • 53
0
votes
1 answer

Requests canceled with server under heavy load hang on IIS

I am facing a difficult-to-reproduce problem that I think is related to the use of CancellationToken. I have three async apps (each on a different server) that communicate through ASP.NET Web API as shown in the diagram below A ---> B ---> C App A…
alextercete
  • 4,871
  • 3
  • 22
  • 36
0
votes
1 answer

Deadlock from combination of async/await, CancellationTokenSource, Threading.Timer

In the process of writing a wrapper to arbitrary cancellable code that's supposed to run while a certain condition holds true (which has to be verified regularly), I came across interesting behavior in the interaction between…
observer
  • 3
  • 2
0
votes
0 answers

How do I queue up and cancel multiple events?

Let's say we have an event that triggers the following method: public static void DoSomeWork(object obj) { CancellationTokenSource cts = (CancellationTokenSource)obj; if (cts.IsCancellationRequested) return; …
Lunyx
  • 3,164
  • 6
  • 30
  • 46
-1
votes
1 answer

How to dispose a LongRunningTask using cancellationTokens

Related to this question: I need to cancel long running loop in signalR, how do i do this? I set up a quick way to better understand the situation for myself and found Half a solution. I'm able to cancel a task (not sure how since the…
-1
votes
1 answer

Is there way to cancel or kill a task which waits for a response from a http client in c#?

since i googled for the last 2 days and still can't figure it out: I'm working with Unity and c#. I have a http client which sends async get requests to my local server. This server is set to always wait a certain amount of time until it sends a…
-1
votes
1 answer

Cancel Long Running Method on Button Click Angular with .Net Core

Am working on Angular 8 with .Net Core, In my page if have two button "Search" and "Cancel Search". Is there a way to abort the Search button request when "Cancel Search" button is clicked? Scenario: Clicking on the Search button, the data is taking…
-1
votes
1 answer

HTTPClientFactory MOQ testing cancellationToken is true when running in normal, but when executing unit test case cancelation token is false

I am trying to make an PostAsync call using IHttpClientFactory , The cancellation token is working fine and exception is caught by CatchBlock. But when trying to mock the method the Cancellation token is false and catch block is not caught Below…
-1
votes
1 answer

IsCancellationRequested = true and go to exception. How to get all respose from API?

I am working on .net core 3.1. I am trying to use https://services.timeanddate.com/ API. when debuggeing I got IsCancellationRequested = true and go to exception. The request that was canceled that line below: using var response = await…
1 2 3
23
24