I have scenario where User is able to abort the jQuery Ajax call, subsequently I want to Cancel MVC Request then Web API request and lastly the SQL procedure execution.
How do I chain the cancellation of request from the browser to MVC Application. The MVC application will now trigger a Web API request which will query the database. we want to cancel any subsequent queries through the whole leg of the process if the requested response is not received.
I googled and found cancellation token of task can help me but I am unable to figure out how to make it work.
looking for example/suggestions on how to implement this.
so far I have tried using Task.Factory.StartNew but its not waiting for results.
var tcs = new TaskCompletionSource<JsonResult>();
//comment this whole this is just used for testing
Task.Factory.StartNew(() =>
{
//Simulate work (usually from 3rd party code)
for (int i = 0; i < 100000; i++)
Console.WriteLine("value" + i);
//execution never comes here till above for loop or
//may be long execution /computation get completed
if (token.IsCancellationRequested)
token.ThrowIfCancellationRequested();
//Console.WriteLine("Task finished!");
}, token);
tcs.SetResult(Json(ringSearch, JsonRequestBehavior.AllowGet));
return tcs.Task;