Movatterモバイル変換


[0]ホーム

URL:


Skip to main contentSkip to in-page navigation

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Parallel Class

Definition

Namespace:
System.Threading.Tasks
Assemblies:
mscorlib.dll, System.Threading.Tasks.Parallel.dll
Assemblies:
netstandard.dll, System.Threading.Tasks.Parallel.dll
Assembly:
System.Threading.Tasks.Parallel.dll
Assembly:
mscorlib.dll
Assembly:
netstandard.dll
Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs
Source:
Parallel.cs

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Provides support for parallel loops and regions.

public ref class Parallel abstract sealed
public static class Parallel
type Parallel = class
Public Class Parallel
Inheritance
Parallel

Examples

This example demonstrates several approaches to implementing a parallel loop using multiple language constructs.

using System.Threading.Tasks;   class Test{    static int N = 1000;    static void TestMethod()    {        // Using a named method.        Parallel.For(0, N, Method2);        // Using an anonymous method.        Parallel.For(0, N, delegate(int i)        {            // Do Work.        });        // Using a lambda expression.        Parallel.For(0, N, i =>        {            // Do Work.        });    }    static void Method2(int i)    {        // Do work.    }}
Imports System.Threading.TasksModule Module1    Sub Main()        Dim N As Integer = 1000        ' Using a named method        Parallel.For(0, N, AddressOf Method2)        ' Using a lambda expression.        Parallel.For(0, N, Sub(i)                               ' Do Work                           End Sub)    End Sub    Sub Method2(ByVal i As Integer)        ' Do work.    End SubEnd Module

Remarks

TheParallel class provides library-based data parallel replacements for common operations such as for loops, for each loops, and execution of a set of statements.

Methods

For(Int32, Int32, Action<Int32,ParallelLoopState>)

Executes afor loop in which iterations may run in parallel and the state of the loop can be monitored and manipulated.

For(Int32, Int32, Action<Int32>)

Executes afor loop in which iterations may run in parallel.

For(Int32, Int32, ParallelOptions, Action<Int32,ParallelLoopState>)

Executes afor loop in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

For(Int32, Int32, ParallelOptions, Action<Int32>)

Executes afor loop in which iterations may run in parallel and loop options can be configured.

For(Int64, Int64, Action<Int64,ParallelLoopState>)

Executes afor loop with 64-bit indexes in which iterations may run in parallel and the state of the loop can be monitored and manipulated.

For(Int64, Int64, Action<Int64>)

Executes afor loop with 64-bit indexes in which iterations may run in parallel.

For(Int64, Int64, ParallelOptions, Action<Int64,ParallelLoopState>)

Executes afor loop with 64-bit indexes in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

For(Int64, Int64, ParallelOptions, Action<Int64>)

Executes afor loop with 64-bit indexes in which iterations may run in parallel and loop options can be configured.

For<TLocal>(Int32, Int32, Func<TLocal>, Func<Int32,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

Executes afor loop with thread-local data in which iterations may run in parallel, and the state of the loop can be monitored and manipulated.

For<TLocal>(Int32, Int32, ParallelOptions, Func<TLocal>, Func<Int32,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

Executes afor loop with thread-local data in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

For<TLocal>(Int64, Int64, Func<TLocal>, Func<Int64,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

Executes afor loop with 64-bit indexes and thread-local data in which iterations may run in parallel, and the state of the loop can be monitored and manipulated.

For<TLocal>(Int64, Int64, ParallelOptions, Func<TLocal>, Func<Int64,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

Executes afor loop with 64-bit indexes and thread-local data in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

ForAsync<T>(T, T, CancellationToken, Func<T,CancellationToken,ValueTask>)

Executes a for loop in which iterations may run in parallel.

ForAsync<T>(T, T, Func<T,CancellationToken,ValueTask>)

Executes a for loop in which iterations may run in parallel.

ForAsync<T>(T, T, ParallelOptions, Func<T,CancellationToken,ValueTask>)

Executes a for loop in which iterations may run in parallel.

ForEach<TSource,TLocal>(IEnumerable<TSource>, Func<TLocal>, Func<TSource,ParallelLoopState,Int64,TLocal,TLocal>, Action<TLocal>)

Executes aforeach (For Each in Visual Basic) operation with thread-local data on anIEnumerable in which iterations may run in parallel and the state of the loop can be monitored and manipulated.

ForEach<TSource,TLocal>(IEnumerable<TSource>, Func<TLocal>, Func<TSource,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

Executes aforeach (For Each in Visual Basic) operation with thread-local data on anIEnumerable in which iterations may run in parallel, and the state of the loop can be monitored and manipulated.

ForEach<TSource,TLocal>(IEnumerable<TSource>, ParallelOptions, Func<TLocal>, Func<TSource,ParallelLoopState,Int64,TLocal,TLocal>, Action<TLocal>)

Executes aforeach (For Each in Visual Basic) operation with thread-local data and 64-bit indexes on anIEnumerable in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

ForEach<TSource,TLocal>(IEnumerable<TSource>, ParallelOptions, Func<TLocal>, Func<TSource,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

Executes aforeach (For Each in Visual Basic) operation with thread-local data on anIEnumerable in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

ForEach<TSource,TLocal>(OrderablePartitioner<TSource>, Func<TLocal>, Func<TSource,ParallelLoopState,Int64,TLocal,TLocal>, Action<TLocal>)

Executes aforeach (For Each in Visual Basic) operation with thread-local data on aOrderablePartitioner<TSource> in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

ForEach<TSource,TLocal>(OrderablePartitioner<TSource>, ParallelOptions, Func<TLocal>, Func<TSource,ParallelLoopState,Int64,TLocal,TLocal>, Action<TLocal>)

Executes aforeach (For Each in Visual Basic) operation with 64-bit indexes and with thread-local data on aOrderablePartitioner<TSource> in which iterations may run in parallel , loop options can be configured, and the state of the loop can be monitored and manipulated.

ForEach<TSource,TLocal>(Partitioner<TSource>, Func<TLocal>, Func<TSource,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

Executes aforeach (For Each in Visual Basic) operation with thread-local data on aPartitioner in which iterations may run in parallel and the state of the loop can be monitored and manipulated.

ForEach<TSource,TLocal>(Partitioner<TSource>, ParallelOptions, Func<TLocal>, Func<TSource,ParallelLoopState,TLocal,TLocal>, Action<TLocal>)

Executes aforeach (For Each in Visual Basic) operation with thread-local data on aPartitioner in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

ForEach<TSource>(IEnumerable<TSource>, Action<TSource,ParallelLoopState,Int64>)

Executes aforeach (For Each in Visual Basic) operation with 64-bit indexes on anIEnumerable in which iterations may run in parallel, and the state of the loop can be monitored and manipulated.

ForEach<TSource>(IEnumerable<TSource>, Action<TSource,ParallelLoopState>)

Executes aforeach (For Each in Visual Basic) operation on anIEnumerable in which iterations may run in parallel, and the state of the loop can be monitored and manipulated.

ForEach<TSource>(IEnumerable<TSource>, Action<TSource>)

Executes aforeach (For Each in Visual Basic) operation on anIEnumerable in which iterations may run in parallel.

ForEach<TSource>(IEnumerable<TSource>, ParallelOptions, Action<TSource,ParallelLoopState,Int64>)

Executes aforeach (For Each in Visual Basic) operation with 64-bit indexes on anIEnumerable in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

ForEach<TSource>(IEnumerable<TSource>, ParallelOptions, Action<TSource,ParallelLoopState>)

Executes aforeach (For Each in Visual Basic) operation on anIEnumerable in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

ForEach<TSource>(IEnumerable<TSource>, ParallelOptions, Action<TSource>)

Executes aforeach (For Each in Visual Basic) operation on anIEnumerable in which iterations may run in parallel and loop options can be configured.

ForEach<TSource>(OrderablePartitioner<TSource>, Action<TSource,ParallelLoopState,Int64>)

Executes aforeach (For Each in Visual Basic) operation on aOrderablePartitioner<TSource> in which iterations may run in parallel and the state of the loop can be monitored and manipulated.

ForEach<TSource>(OrderablePartitioner<TSource>, ParallelOptions, Action<TSource,ParallelLoopState,Int64>)

Executes aforeach (For Each in Visual Basic) operation on aOrderablePartitioner<TSource> in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

ForEach<TSource>(Partitioner<TSource>, Action<TSource,ParallelLoopState>)

Executes aforeach (For Each in Visual Basic) operation on aPartitioner in which iterations may run in parallel, and the state of the loop can be monitored and manipulated.

ForEach<TSource>(Partitioner<TSource>, Action<TSource>)

Executes aforeach (For Each in Visual Basic) operation on aPartitioner in which iterations may run in parallel.

ForEach<TSource>(Partitioner<TSource>, ParallelOptions, Action<TSource,ParallelLoopState>)

Executes aforeach (For Each in Visual Basic) operation on aPartitioner in which iterations may run in parallel, loop options can be configured, and the state of the loop can be monitored and manipulated.

ForEach<TSource>(Partitioner<TSource>, ParallelOptions, Action<TSource>)

Executes aforeach (For Each in Visual Basic) operation on aPartitioner in which iterations may run in parallel and loop options can be configured.

ForEachAsync<TSource>(IAsyncEnumerable<TSource>, CancellationToken, Func<TSource,CancellationToken,ValueTask>)

Executes afor-each operation on anIEnumerable<T> in which iterations may run in parallel.

ForEachAsync<TSource>(IAsyncEnumerable<TSource>, Func<TSource,CancellationToken,ValueTask>)

Executes afor-each operation on anIEnumerable<T> in which iterations may run in parallel.

ForEachAsync<TSource>(IAsyncEnumerable<TSource>, ParallelOptions, Func<TSource,CancellationToken,ValueTask>)

Executes afor-each operation on anIEnumerable<T> in which iterations may run in parallel.

ForEachAsync<TSource>(IEnumerable<TSource>, CancellationToken, Func<TSource,CancellationToken,ValueTask>)

Executes afor-each operation on anIEnumerable<T> in which iterations may run in parallel.

ForEachAsync<TSource>(IEnumerable<TSource>, Func<TSource,CancellationToken,ValueTask>)

Executes afor-each operation on anIEnumerable<T> in which iterations may run in parallel.

ForEachAsync<TSource>(IEnumerable<TSource>, ParallelOptions, Func<TSource,CancellationToken,ValueTask>)

Executes afor-each operation on anIEnumerable<T> in which iterations may run in parallel.

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.

Applies to

Thread Safety

All public and protected members ofParallel are thread-safe and may be used concurrently from multiple threads.

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?