在并行处理时完成后立即重新启动任务

发布于 2025-01-30 06:02:36 字数 2166 浏览 2 评论 0原文

完成后,我必须立即重新启动所有任务。请在下面查看我的程序;

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝戈者 2025-02-06 06:02:36

您的程序当前同时运行这三个任务,(异步)等待它们都完成,然后在循环中执行此操作。

如果要在循环中运行每个任务,则只需移动循环:

static async Task ProcessTasksAsync()
{
  Task<int> taskA = LoopA(10);
  Task<int> taskB = LoopB(10);
  Task<int> taskC = LoopC(10);

  await Task.WhenAll(taskA, taskB, taskC);
}

static async Task LoopA()
{
  while (true)
    await AwaitAndProcessAsync(LongTaskA(10));
}

static async Task LoopB()
{
  while (true)
    await AwaitAndProcessAsync(MediumTaskB(10));
}

static async Task LoopC()
{
  while (true)
    await AwaitAndProcessAsync(ImmediateTaskC(10));
}

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:

static async Task ProcessTasksAsync()
{
  Task<int> taskA = LoopA(10);
  Task<int> taskB = LoopB(10);
  Task<int> taskC = LoopC(10);

  await Task.WhenAll(taskA, taskB, taskC);
}

static async Task LoopA()
{
  while (true)
    await AwaitAndProcessAsync(LongTaskA(10));
}

static async Task LoopB()
{
  while (true)
    await AwaitAndProcessAsync(MediumTaskB(10));
}

static async Task LoopC()
{
  while (true)
    await AwaitAndProcessAsync(ImmediateTaskC(10));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文