我正在尝试构建一种调度程序(这可能不是相关术语),它将按顺序运行多个任务。
这是我的 POC 代码(请忽略队列/出队机制,它很差,但我猜不是这里的问题)
static void Main(string[] args)
{
ProcessingQueue o_q = new ProcessingQueue();
o_q.Enqueue(async () => { await SimulateTaskSequence(1); });
o_q.Enqueue(async () => { await SimulateTaskSequence(2); });
Console.ReadLine();
}
public static async Task SimulateTaskSequence(int taskNbr)
{
Console.WriteLine("T{0} - Working 1sec", taskNbr);
Thread.Sleep(1000);
Console.WriteLine("T{0} - Zzz 1st 1sec", taskNbr);
await Task.Delay(1000);
Console.WriteLine("T{0} - Working 1sec", taskNbr);
Thread.Sleep(1000);
Console.WriteLine("T{0} - Done", taskNbr);
}
public class ProcessingQueue
{
Queue<Action> _Queue = new Queue<Action>();
private bool _stillRunning = false;
public void Enqueue(Action a)
{
lock (_Queue)
{
_Queue.Enqueue(a);
if (_stillRunning == false)
{
StartProcessing();
}
}
}
private void StartProcessing()
{
_stillRunning = true;
Task.Run(async () =>
{
Action a = null;
while (true)
{
lock (_Queue)
{
if (_Queue.Any() == true)
{
a = _Queue.Dequeue();
}
else
{
break;
}
}
await Task.Run(a); //how to wait for all subtasks!!???
}
_stillRunning = false;
});
}
我的问题是,一旦第一个任务(T1)的第一个等待发生,第二个任务(T2)就开始执行。
我得到以下输出:
T1 - Working 1sec
T1 - Zzz 1st 1sec
T2 - Working 1sec
T2 - Zzz 1st 1sec
T1 - Working 1sec
T1 - Done
T2 - Working 1sec
T2 - Done
但我期待的是:
T1 - Working 1sec
T1 - Zzz 1st 1sec
T1 - Working 1sec
T1 - Done
T2 - Working 1sec
T2 - Zzz 1st 1sec
T2 - Working 1sec
T2 - Done
我理解为什么这是默认行为,但我需要改变它。我在新的 TaskFactory 中尝试使用 TaskContinuationOptions 和 TaskCreationOptions,但没有更好的结果。 这可能吗?
我建议创建一个
ProcessingQueue<Func<Task>>
而不是 ProcessingQueue<Action>
public class ProcessingQueue
{
Queue<Func<Task>> _Queue = new Queue<Func<Task>>();
private bool _stillRunning = false;
public void Enqueue(Func<Task> a)
{
lock (_Queue)
{
_Queue.Enqueue(a);
if (_stillRunning == false)
{
StartProcessing();
}
}
}
private void StartProcessing()
{
_stillRunning = true;
Task.Run(async () =>
{
Func<Task> a = null;
while (true)
{
lock (_Queue)
{
if (_Queue.Any() == true)
{
a = _Queue.Dequeue();
}
else
{
break;
}
}
await a(); //how to wait for all subtasks!!???
}
_stillRunning = false;
});
}
解释
在有问题的代码中,
Action a;
...
await Task.Run(a);
您正在执行
Task.Run(Action action)
,因为操作可能包含异步任务,因此 Run
方法不会等待任务,因为没有任务。当您调用 Task.Run(Func<Task> task)
Run
方法时知道它是任务并且它将等待它,