如何将多个任务添加到并行。不进行硬编码的情况下进行浏览?

发布于 2025-02-13 14:07:18 字数 1725 浏览 1 评论 0原文

我正在尝试了解并行。 参数)。

在以下代码示例中,静态方法和lambda方法的并行调用以及一个或多个内联委托会发生。假设我想并行另外5个任务,然后我将不得不将其作为代表(与假设我使用并行使用

static void Main()
        {
            try
            {
                Parallel.Invoke(
                    BasicAction,    // Param #0 - static method
                    () =>           // Param #1 - lambda expression
                    {
                        Console.WriteLine("Method=beta, Thread={0}", Thread.CurrentThread.ManagedThreadId);
                    },
                    delegate()      // Param #2 - in-line delegate
                    {
                        Console.WriteLine("Method=gamma, Thread={0}", Thread.CurrentThread.ManagedThreadId);
                    }
                );
            }
            // No exception is expected in this example, but if one is still thrown from a task,
            // it will be wrapped in AggregateException and propagated to the main thread.
            catch (AggregateException e)
            {
                Console.WriteLine("An action has thrown an exception. THIS WAS UNEXPECTED.\n{0}", e.InnerException.ToString());
            }
        }

        static void BasicAction()
        {
            Console.WriteLine("Method=alpha, Thread={0}", Thread.CurrentThread.ManagedThreadId);
        }
    }

。代码> console.writeline(“ method = beta,thread = {0}”,thread.currentthread.managedthreadid); - 内部的文本console.writeline可以被参数化,然后我们可以使用一个并行。foreach循环循环,并调用console.writeline。 > Parallel.Invoke ?

I am trying to understand the use-case for Parallel.Invoke which can take either array of actions or colon separated individual actions (since it uses params).

In the following code sample, parallel invocation happens for the static method, and lambda method, and one or more inline delegates. Suppose there are 5 more tasks that I want to run in parallel, then I will have to hardcode it as delegates (unlike suppose I had used Parallel.ForEach. How to add multiple tasks to the Parallel.Invoke without hardcoding?

static void Main()
        {
            try
            {
                Parallel.Invoke(
                    BasicAction,    // Param #0 - static method
                    () =>           // Param #1 - lambda expression
                    {
                        Console.WriteLine("Method=beta, Thread={0}", Thread.CurrentThread.ManagedThreadId);
                    },
                    delegate()      // Param #2 - in-line delegate
                    {
                        Console.WriteLine("Method=gamma, Thread={0}", Thread.CurrentThread.ManagedThreadId);
                    }
                );
            }
            // No exception is expected in this example, but if one is still thrown from a task,
            // it will be wrapped in AggregateException and propagated to the main thread.
            catch (AggregateException e)
            {
                Console.WriteLine("An action has thrown an exception. THIS WAS UNEXPECTED.\n{0}", e.InnerException.ToString());
            }
        }

        static void BasicAction()
        {
            Console.WriteLine("Method=alpha, Thread={0}", Thread.CurrentThread.ManagedThreadId);
        }
    }

For example - Console.WriteLine("Method=beta, Thread={0}", Thread.CurrentThread.ManagedThreadId); - the text inside Console.WriteLine can be parameterized and then we can use a Parallel.Foreach to loop over an array of strings and invoke the Console.WriteLine. Can this be done with Parallel.Invoke?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

避讳 2025-02-20 14:07:18

Parallel.Invoke Overloads具有参数[]作为参数。因此,可以将简单的动作传递给该方法。

List<Action> actions = new List<Action>();
actions.Add(() => Console.WriteLine("Method 1"));
actions.Add(() => Console.WriteLine("Method2"));
// add more actions based on some conditions

Parallel.Invoke(actions.ToArray());

One of the Parallel.Invoke overloads has params Action[] as parameter. So it is possible to pass a simple array of actions to the method.

List<Action> actions = new List<Action>();
actions.Add(() => Console.WriteLine("Method 1"));
actions.Add(() => Console.WriteLine("Method2"));
// add more actions based on some conditions

Parallel.Invoke(actions.ToArray());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文