I have used CancellationToken
in ActionFilter
to cancel a task if user closes the tab after GET request (as this is heavy task i.e. takes time 20 sec and memory also).
if (!token.IsCancellationRequested)
{
token.Register(() => { throw new OperationCanceledException(token); });
}
In Controller action I have GetAsync
method which does the heavy calculation.
Now when GET request is hit from browser and after 5 sec I closed the browser tab, it throws the Exception and Continues the execution till completion. I want it to stop the processing further and release memory and CPU.
I have used the breakpoint after cancelling the task , it stills calculates the results(18,00,000 results) which is no use and also cancelling has no effect except the exception thrown.
I also can't pass the token to Controller and do some stuff there as due to Open/Close principle.
I tried Environment.Exit(0)
but it closes the IIS Express and stop the server.
Please tell me a method to release memory and CPU usage as soon as cancellation exception thrown.