检查任务是否已经运行,然后开始新的。

时间:2022-04-12 13:51:13

There is a process which is executed in a task. I do not want more than one of these to execute simultaneously.

有一个进程在任务中执行。我不希望同时执行其中的多个。

Is this the correct way to check to see if a task is already running?

这是检查任务是否已经在运行的正确方法吗?

private Task task;

public void StartTask()
{
    if (task != null && (task.Status == TaskStatus.Running || task.Status == TaskStatus.WaitingToRun || task.Status == TaskStatus.WaitingForActivation))
    {
        Logger.Log("Task has attempted to start while already running");
    }
    else
    {
        Logger.Log("Task has began");

        task = Task.Factory.StartNew(() =>
        {
            // Stuff                
        });
    }
}

3 个解决方案

#1


30  

As suggested by Jon Skeet, the Task.IsCompleted is the better option.

正如Jon Skeet所建议的,任务。IsCompleted是更好的选择。

According to MSDN:

根据MSDN:

IsCompleted will return true when the task is in one of the three final states: RanToCompletion, Faulted, or Canceled.

当任务处于三种最终状态之一时,IsCompleted将返回true: RanToCompletion、Faulted或cancel。

But it appears to return true in the TaskStatus.WaitingForActivation state too.

但在任务状态中它似乎返回true。WaitingForActivation状态。

#2


5  

private Task task;

public void StartTask()
{
    if ((task != null) && (task.IsCompleted == false ||
                           task.Status == TaskStatus.Running ||
                           task.Status == TaskStatus.WaitingToRun ||
                           task.Status == TaskStatus.WaitingForActivation))
    {
        Logger.Log("Task is already running");
    }
    else
    {
        task = Task.Factory.StartNew(() =>
        {
            Logger.Log("Task has been started");
            // Do other things here               
        });
    }
}

#3


2  

You can check it with:

你可以查阅:

if ((taskX == null) || (taskX.IsCompleted))
{
   // start Task
   taskX.Start();
   //or
   taskX = task.Factory.StartNew(() =>
   {
      //??
   }
}

#1


30  

As suggested by Jon Skeet, the Task.IsCompleted is the better option.

正如Jon Skeet所建议的,任务。IsCompleted是更好的选择。

According to MSDN:

根据MSDN:

IsCompleted will return true when the task is in one of the three final states: RanToCompletion, Faulted, or Canceled.

当任务处于三种最终状态之一时,IsCompleted将返回true: RanToCompletion、Faulted或cancel。

But it appears to return true in the TaskStatus.WaitingForActivation state too.

但在任务状态中它似乎返回true。WaitingForActivation状态。

#2


5  

private Task task;

public void StartTask()
{
    if ((task != null) && (task.IsCompleted == false ||
                           task.Status == TaskStatus.Running ||
                           task.Status == TaskStatus.WaitingToRun ||
                           task.Status == TaskStatus.WaitingForActivation))
    {
        Logger.Log("Task is already running");
    }
    else
    {
        task = Task.Factory.StartNew(() =>
        {
            Logger.Log("Task has been started");
            // Do other things here               
        });
    }
}

#3


2  

You can check it with:

你可以查阅:

if ((taskX == null) || (taskX.IsCompleted))
{
   // start Task
   taskX.Start();
   //or
   taskX = task.Factory.StartNew(() =>
   {
      //??
   }
}