I have a piece of code which does some processing of data, written in c#.
string status;
log.Info("Starting the Process");
StartProcessing(); // takes 10-12 mins usually
var task = Task.Run(() =>
{
do
{
status = GetStatus();
log.Info("Status inside loop : " + status);
} while (status != "Complete")
});
if (task.Wait(TimeSpan.FromMinutes(Convert.ToDouble(15))))
{
log.Info("Processing finished");
}
else
{
log.Info("Process did not complete in 15 mins");
log.Info("Stopping the Process");
StopProcessing(); // this takes 2-3 mins usually.
log.Info("Process stopped");
}
StartProcessing() method actually start some background processing of the data. It doesn't return any value or wait for the method to finish. So we added a do-while loop to check the status of the processing. If the status is complete , then come of the loop and proceed further.
Now the requirement has changed to put a timeout for the processing. If the processing is taking more than 5 mins, then we have to stop the process. So I have wrapped my code in Task.Run as shown above and written a if else condition to check the time.
This doesn't seem to work as expected, because when I run my code, this is log information I'm getting.
Starting the Process
Status inside loop : Processing
Status inside loop : Processing
Status inside loop : Processing
Status inside loop : Processing --> this line is repeated multiple times with in 15 mins.
Process did not complete in 15 mins.
Stopping the Process
Status inside loop : Processing
Status inside loop : Processing
Status inside loop : Processing --> why is it going back to do while after coming out ?
Process stopped
The execution is going back to do while even after coming out. Is there anything wrong am I doing here?
Any suggestions are very helpful.