Task stays in WaitingToRun state for abnormally long time

元气小坏坏 提交于 2021-02-18 17:44:10

问题


I've got a program that handles a variety of tasks running in parallel. A single task acts as a manager of sorts, making sure certain conditions are met before the next task is ran. However, I've found that sometimes a task will sit in the WaitingToRun state for a very long time. Here's the following code:

mIsDisposed = false;
mTasks      = new BlockingCollection<TaskWrapper>(new ConcurrentQueue<TaskWrapper>());

Task.Factory.StartNew(() => {
    while (!mIsDisposed) {
         var tTask = mTasks.Take();
         tTask.task.Start();
         while (tTask.task.Status == TaskStatus.WaitingToRun) {
             Console.WriteLine("Waiting to run... {0}", tTask.task.Id);
             Thread.Sleep(200);
         }
         tTask.ready.Wait();
     }
     mTasks.Dispose();
});

DoWork();
DoWork();
DoWork();
DoWork();
DoWorkAsync();
DoWorkAsync();
DoWorkAsync();
DoWorkAsync();
DoWorkAsync();
DoWork();

TaskWrapper is very simply defined as:

private class TaskWrapper
{
    public Task task  { get; set; }
    public Task ready { get; set; }
}

And tasks are only currently added in 2 places:

public void DoWork()
{
    DoWorkAsync().Wait();
}

public Task DoWorkAsync()
{
    ManualResetEvent next = new ManualResetEvent(false);

    Task task  = new Task(() => ActualWork(next));
    Task ready = Task.Factory.StartNew(() => next.Wait());

    mTasks.Add(new TaskWrapper() {
        task  = task,
        ready = ready
    });
    return task;
}

Where ActualWork(next) calls next.Set().

This queues work and waits until next has been set before allowing the next work item to proceed. You can either wait for the entire task to finish before continuing by calling DoWork() or queue multiple tasks at once (which are supposed to run after next has been set).

However, when adding a task via DoWorkAsync(), after calling tTask.task.Start(), tTask.task sits in the WaitingToRun state for a loooong time (like 30 seconds to a minute), then magically starts running. I've monitored this using the while loop, and Waiting To Run... # will display for quite some time.

Calling DoWork() always runs immediately. I'm sure this has something to do with calling Wait on the task that is set to run.

I'm at a loss, here.

UPDATE:

I've managed to make the code work, but I'd still like to know why there's an issue in the first place.

After some experimental changes, I've managed to fix my own problem, but it's more of a "Oh, so I just can't do that" rather than a good fix. It turns out my problem was enqueuing tasks to run too quickly. By modifying DoWorkAsync() to no longer use Task.Factory.StartNew and changing tTask.ready.Wait() to tTask.ready.RunSynchronously I've managed to solve my issue.

Is there a reason the TaskScheduler is delaying the scheduling of my tasks? Am I saturating some underlying resources? What's going on here?


回答1:


The threads will be run in the system's thread pool. The thread pool has a minimum number of threads available at all times (see ThreadPool.SetMinThreads()). If you try to create more than that many threads, a delay of approximately 500ms will be introduced between each new thread starting.

There is also a maximum number of threads in the thread pools (see ThreadPool.GetMaxThreads()), and if you reach that limit no new threads will be created; it will wait until an old thread dies before scheduling a new one (or rather, rescheduling the old one to run your new thread, of course).

You are unlikely to be hitting that limit though - it's probably over 1000.




回答2:


Just faced similar issue.

I have a bunch of similar tasks running inifite loops, one of that tasks from time to time stays in WaitingToRun state permamently.

Creating tasks in that way did the trick for me:

_task = new Task(() => DoSmth(_cancellationTokenSource.Token), TaskCreationOptions.LongRunning);
_task.Start();



回答3:


Ok, I've just been faced with a similar issue. A bit of code that created and started a task ran, but the task never started (it just changed status to WaitingToRun)

Having tried the other options in this thread to no avail I thought about it a bit more, and realised that the code that was calling this method was itself called in a continuation task, that had been specified to run on the UI task scheduler (As it needed to update the UI)...

So something like

void Main()
{
    var t1 = new Task(() => Console.WriteLine("hello, I'm task t1"));
    t1.ContinueWith(t => CreateAndRunASubTask(), TaskScheduler.FromCurrentSynchronizationContext());
    t1.Start();

    Console.WriteLine("All tasks done with");
}

// Define other methods and classes here
public void CreateAndRunASubTask()
{
    var tsk = new Task(() => Console.WriteLine("hello, I'm the sub-task"));
    tsk.Start();
    Console.WriteLine("sub-task has been told to start");
    tsk.Wait();
    // the code blocks on tsk.Wait() indefinately, the tsk status being "WaitingToRun"
    Console.WriteLine("sub-task has finished");
}

The fix turned out to be pretty simple - when specifying the continuation task you need to specify the TaskContinuationOption: TaskContinuationOptions.HideScheduler

This has the effect of... (taken from the XML comment)

Specifies that tasks created by the continuation by calling methods such as System.Threading.Tasks.Task.Run(System.Action) or System.Threading.Tasks.Task.ContinueWith(System.Action{System.Threading.Tasks.Task}) see the default scheduler (System.Threading.Tasks.TaskScheduler.Default) rather
than the scheduler on which this continuation is running as the current scheduler.

ie (in my example)

t1.ContinueWith(t => 
    CreateAndRunASubTask(), 
    System.Threading.CancellationToken.None, 
    TaskContinuationOptions.HideScheduler,
    TaskScheduler.FromCurrentSynchronizationContext());

Hope this helps someone, as it stumped me for a good while!



来源:https://stackoverflow.com/questions/14640984/task-stays-in-waitingtorun-state-for-abnormally-long-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!