In this article, we are going to learn how to cancel or interrupt the Long Running Task using the Cancellationtokensource method in .NET 4.0.
So, here is the task: My web application will send POST request by making web service API call and get the data via a third-party library. For example, consider, the application is going to send details about the students through the API and will get the response once the POST data is successfully received and saved on another side. Consider that the application is going to send 5000 bits of data through the API. So, by simply using for-each loop by calling third-party API, we can POST 5000 data.
It will take too long to finish this job. Until this job is done the application will not allow other operations to perform as the job is running in the main thread. To make it possible, we will move to “TASK”, to run this job in a separate thread so that the application can do other operations. To understand this, have a look at the code below.
- Var task=Task.Factory.StartNew(()=> CallWebServiceandGetResponceData());
The code is creating the task which is making API webservice call inside CallWebServiceandGetResponceData function to send a request and get the response data. So by doing this, the application allows this task operation in a separate thread and we can do other operations in the main thread, which will not affect the task operation. Everything is going fine.
If you are using .NET 4.5 Framework you can use Async and wait for keywords to do this. But in .NET 4.0 we can’t use async and await keywords, so I'm going to use “TASK” to achieve this operation.
So, now I need to cancel or stop the task whenever I need to. For example, 2673rd data is currently running inside the CallWebServiceandGetResponceData function, when I hit the stop button I need to cancel or stop calling third-party API and exit the loop. To do this I'm going to use CancellationToken.
.NET 4.0 introduces a lightweight struct called the cancellationToken that provide a mechanism to cancel synchronous and asynchronous operations. CancellationToken consist is a CancellationRequested method, it is a Boolean property that can be polled by code to find out if it has been canceled or the cancellation has been requested.
By using instance of CancellationTokenSource class to get a cancellation token, in cancellation model the cancellation token can be passed freely to the listeners, so that canceling token is cleanly separted from the ability to consume a request for cancellation. Here is the sample code to cancel a task,

Hadshana KamalanathanPosted Jul 19, 2018, 4:47 AM
Thanks for sharing
Amine TouahriaPosted Jul 18, 2018, 3:00 AM
CancellationTokenSource mCancellationTokenSource = new CancellationTokenSource();CancellationToken token = mCancellationTokenSource.Token; Task<string> ObjTask = Task.Factory.StartNew(() => { while (!token.IsCancellationRequested) { try { for (int i = 0; i < 10000; i++) { if (i == 2341) { mCancellationTokenSource.Cancel(); } // Sending data to other end by api call } } catch { return "KO"; } } return "OK"; }, token);
Mohamed IbrahimPosted Jul 17, 2018, 5:08 PM
Thanx for sharing sir .. can i ask a question to you .. i has a reports that takes a very long time how can i overcome this problem ? .. because the user getting bored of this reports and need to spent As little as possible time