0

I have a Task which is as below :

CancellationTokenSource cts = new CancellationTokenSource();
var token = cts.Token;

var task = Task.Run(() =>
{
 do {
    Console.log("Check status");
    if (cts.IsCancellationRequested)
    {
        break;
    }
    Thread.Sleep(5*60*1000);
    status =  GetStatus();
    Console.log("status in while loop : " + status);
    if (status == "Complete")
    {
        flag = 1;
        break;
    } 
 
 } while (flag == 1)

  Console.log("status after while: " +  status);
  
  // some other businness logic

},token);

if (task.Wait(System.TimeSpan.FromMinutes(1))){
 Console.log ("Success");
}
else{
 Console.log ("Timed out");
 cts.Cancel();
 StopExecution();
}


    void Stop Execution(){
     Console.log ("Stopping Execution");
     // logic to stop
     
     Console.log("Stopped Successfully");
    
    }

I have written the above code , to run some task for some time and cancel it when the specified time is up.
The problem with the above code is , when ever there is time out happening , it comes to the else block , but it is not cancelling the task which is running.
So I'll show the execution flow with the help of logs :

CheckStatus
status in while loop : InProgress
CheckStatus
status in while loop : InProgress
Timed out
Stopping Execution
CheckStatus
Stopped Successfully
status after while: InProgress

What I observe is even after task is timed-out , it is still executing the task.
My requirement is once the time is up , it should end the entire task and should not be executed at all. In this example I have shown just the do while logic , but there is some other business logic as well.
I want the entire task execution to be stopped and proceed further. Can anyone point me what am I doing wrong to achieve that?

CrazyCoder
  • 2,194
  • 10
  • 44
  • 91
  • 3
    Where do you request cancelation in your code? – SᴇM Nov 22 '21 at 13:26
  • 2
    CancellationTokens don't cancel anything, they just provide a mechanism to cancel – Liam Nov 22 '21 at 13:30
  • 1
    Does this answer your question? [Cancellation token and thread not working](https://stackoverflow.com/questions/22273463/cancellation-token-and-thread-not-working) – Liam Nov 22 '21 at 13:30
  • 1
    `Thread.Sleep(5*60*1000);` That's a guaranteed 5 minutes during which it won't be possible to cancel the task. – Matthew Watson Nov 22 '21 at 13:41
  • I suggest changing the sleep call to `if (cts.WaitHandle.WaitOne(TimeSpan.FromMinutes(5))) break;` – Matthew Watson Nov 22 '21 at 13:47
  • @SᴇM , I have updated the question to add cts.Cancel() in the else part. I missed that line before. – CrazyCoder Nov 22 '21 at 14:32
  • Instead of suggesting to close the question, Can anyone please point me to what am I doing wrong ? I would really appreciate that. – CrazyCoder Nov 22 '21 at 14:33
  • You keep using that cancellation token there... [A Tour of Task, Part 9: Delegate Tasks](https://blog.stephencleary.com/2015/03/a-tour-of-task-part-9-delegate-tasks.html) – Theodor Zoulias Nov 22 '21 at 14:54

0 Answers0