อ่านในภาษาอังกฤษ แก้ไข

แชร์ผ่าน


Parallel.Invoke Method

Definition

Executes each of the provided actions, possibly in parallel.

Overloads

Invoke(Action[])

Executes each of the provided actions, possibly in parallel.

Invoke(ParallelOptions, Action[])

Executes each of the provided actions, possibly in parallel, unless the operation is cancelled by the user.

Invoke(Action[])

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

Executes each of the provided actions, possibly in parallel.

C#
public static void Invoke(params Action[] actions);

Parameters

actions
Action[]

An array of Action to execute.

Exceptions

The actions argument is null.

The exception that is thrown when any action in the actions array throws an exception.

The actions array contains a null element.

Examples

This example demonstrates how to use the Invoke method with other methods, anonymous delegates, and lambda expressions.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

    class ParallelInvokeDemo
    {

        // Demonstrated features:
        // 		Parallel.Invoke()
        // Expected results:
        // 		The threads on which each task gets executed may be different.
        //		The thread assignments may be different in different executions.
        //		The tasks may get executed in any order.
        // Documentation:
        //		http://msdn.microsoft.com/library/dd783942(VS.100).aspx
        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);
        }
    }

Remarks

This method can be used to execute a set of operations, potentially in parallel.

No guarantees are made about the order in which the operations execute or whether they execute in parallel. This method does not return until each of the provided operations has completed, regardless of whether completion occurs due to normal or exceptional termination.

For more information, see How to: Use Parallel.Invoke to Execute Parallel Operations.

Applies to

.NET 10 และรุ่นอื่นๆ
ผลิตภัณฑ์ เวอร์ชัน
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0

Invoke(ParallelOptions, Action[])

Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

Executes each of the provided actions, possibly in parallel, unless the operation is cancelled by the user.

C#
public static void Invoke(System.Threading.Tasks.ParallelOptions parallelOptions, params Action[] actions);

Parameters

parallelOptions
ParallelOptions

An object that configures the behavior of this operation.

actions
Action[]

An array of actions to execute.

Exceptions

The CancellationToken in the parallelOptions is set.

The actions argument is null.

-or-

The parallelOptions argument is null.

The exception that is thrown when any action in the actions array throws an exception.

The actions array contains a null element.

The CancellationTokenSource associated with the CancellationToken in the parallelOptions has been disposed.

Remarks

This method can be used to execute a set of operations, potentially in parallel. The cancellation token passed in with the ParallelOptions structure enables the caller to cancel the entire operation. For more information, see Cancellation in Managed Threads.

No guarantees are made about the order in which the operations execute or whether they execute in parallel. This method does not return until each of the provided operations has completed, regardless of whether completion occurs due to normal or exceptional termination.

For more information, see How to: Use Parallel.Invoke to Execute Parallel Operations.

Applies to

.NET 10 และรุ่นอื่นๆ
ผลิตภัณฑ์ เวอร์ชัน
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1
UWP 10.0