在并行处理时完成后立即重新启动任务
完成后,我必须立即重新启动所有任务。请在下面查看我的程序;
class Program
{
static async Task Main(string[] args)
{
int _count = 1;
while (true)
{
await ProcessTasksAsync();
Console.Write("----Restart Tasks----" + (_count++).ToString() + Environment.NewLine);
await Task.Delay(2000);
}
}
static async Task ProcessTasksAsync()
{
Task<int> taskA = LongTaskA(10);
Task<int> taskB = MediumTaskB(10);
Task<int> taskC = ImmediateTaskC(10);
var tasks = new[] { taskA, taskB, taskC };
var processingTasks = tasks.Select(AwaitAndProcessAsync).ToList();
await Task.WhenAll(processingTasks);
}
static async Task AwaitAndProcessAsync(Task<int> task)
{
var result = await task;
Console.WriteLine("Return Result: " + result);
}
static async Task<int> LongTaskA(int _processingCount)
{
for (int i = 0; i < _processingCount; i++)
{
Console.Write(string.Format("LongTaskA:{0}", i.ToString()) + Environment.NewLine);
await Task.Delay(7000);
}
return 1;
}
static async Task<int> MediumTaskB(int _processingCount)
{
for (int i = 0; i < _processingCount; i++)
{
Console.Write(string.Format("MediumTaskB:{0}", i.ToString()) + Environment.NewLine);
await Task.Delay(3000);
}
return 2;
}
static async Task<int> ImmediateTaskC(int _processingCount)
{
for (int i = 0; i < _processingCount; i++)
{
Console.Write(string.Format("ImmediateTaskC:{0}", i.ToString()) + Environment.NewLine);
await Task.Delay(1000);
}
return 3;
}
}
任务A,B&amp; C运行可行。但是,即使完成后,某些任务的完成也不会重新启动。当我使用“ wheral”时,它等待完成另一个任务。
我想在完成后重新启动每个任务。我该如何实现?
谢谢, Shenu
I have to restart all task immediately after completion of the same. Please have a look into my program below;
class Program
{
static async Task Main(string[] args)
{
int _count = 1;
while (true)
{
await ProcessTasksAsync();
Console.Write("----Restart Tasks----" + (_count++).ToString() + Environment.NewLine);
await Task.Delay(2000);
}
}
static async Task ProcessTasksAsync()
{
Task<int> taskA = LongTaskA(10);
Task<int> taskB = MediumTaskB(10);
Task<int> taskC = ImmediateTaskC(10);
var tasks = new[] { taskA, taskB, taskC };
var processingTasks = tasks.Select(AwaitAndProcessAsync).ToList();
await Task.WhenAll(processingTasks);
}
static async Task AwaitAndProcessAsync(Task<int> task)
{
var result = await task;
Console.WriteLine("Return Result: " + result);
}
static async Task<int> LongTaskA(int _processingCount)
{
for (int i = 0; i < _processingCount; i++)
{
Console.Write(string.Format("LongTaskA:{0}", i.ToString()) + Environment.NewLine);
await Task.Delay(7000);
}
return 1;
}
static async Task<int> MediumTaskB(int _processingCount)
{
for (int i = 0; i < _processingCount; i++)
{
Console.Write(string.Format("MediumTaskB:{0}", i.ToString()) + Environment.NewLine);
await Task.Delay(3000);
}
return 2;
}
static async Task<int> ImmediateTaskC(int _processingCount)
{
for (int i = 0; i < _processingCount; i++)
{
Console.Write(string.Format("ImmediateTaskC:{0}", i.ToString()) + Environment.NewLine);
await Task.Delay(1000);
}
return 3;
}
}
The Tasks A, B & C runs parallelly. But, even though the completion of certain task doesn't get restart after completion of the same. As i used "Whenall" it waits to complete the other task.
I want to restart each task after completion of the same. How can i achieve that?
Thanks,
Shenu
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的程序当前同时运行这三个任务,(异步)等待它们都完成,然后在循环中执行此操作。
如果要在循环中运行每个任务,则只需移动循环:
Your program currently runs the three tasks concurrently, (asynchronously) waits for them all to complete, and then does that in a loop.
If you want to run each task in a loop, then you just move the loop: