Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# check if task is running

I need to be able to check if a specific task is running:

            Task.Run(() =>
                {
                    int counter = 720;
                    int sleepTime = 7000;
                    int operationId = 0;
                    Thread.CurrentThread.Name = "GetTasksStatusAsync";
......

so in my code somewhere in another class I need to check "GetTasksStatusAsync" is running. thanks

like image 484
ShaneKm Avatar asked Nov 23 '12 11:11

ShaneKm


2 Answers

How about

Task t = Task.Run(() => ...);

if(t.Status.Equals(TaskStatus.Running))
{
    //task is running
}

Basically I would store my tasks somewhere and make them accessible for other classes. Then you can check the status of the task with the code above. Refer to the TaskStatus-Documentation.

like image 171
KeyNone Avatar answered Oct 09 '22 13:10

KeyNone


This is what worked for me.

Task t = Task.Run(() => ...);

if(t.IsCompleted.Equals(false))  // or if(t.Status.Equals(TaskStatus.WaitingForActivation)
{
}
like image 4
icernos Avatar answered Oct 09 '22 12:10

icernos