C# Task.Parallel
2022/6/16 1:20:06
本文主要是介绍C# Task.Parallel,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
此示例演示了使用多种语言构造实现并行循环的几种方法。1 using System.Threading.Tasks; 2 class Test 3 { 4 static int N = 1000; 5 6 static void TestMethod() 7 { 8 // Using a named method. 9 Parallel.For(0, N, Method2); 10 11 // Using an anonymous method. 12 Parallel.For(0, N, delegate(int i) 13 { 14 // Do Work. 15 }); 16 17 // Using a lambda expression. 18 Parallel.For(0, N, i => 19 { 20 // Do Work. 21 }); 22 } 23 24 static void Method2(int i) 25 { 26 // Do work. 27 } 28 }View Code
也有其他的循环
For(Int32, Int32, Action<Int32,ParallelLoopState>) |
执行 |
For(Int32, Int32, Action<Int32>) |
执行 |
For(Int32, Int32, ParallelOptions, Action<Int32,ParallelLoopState>) |
执行 |
For(Int32, Int32, ParallelOptions, Action<Int32>) |
执行 |
For(Int64, Int64, Action<Int64,ParallelLoopState>) |
执行具有 64 位索引的 |
For(Int64, Int64, Action<Int64>) |
执行具有 64 位索引的 |
For(Int64, Int64, ParallelOptions, Action<Int64,ParallelLoopState>) |
执行具有 64 位索引的 |
For(Int64, Int64, ParallelOptions, Action<Int64>) |
执行具有 64 位索引的 |
For<TLocal>(Int32, Int32, Func<TLocal>, Func<Int32,ParallelLoopState,TLocal,TLocal>, Action<TLocal>) |
执行具有线程本地数据的 |
For<TLocal>(Int32, Int32, ParallelOptions, Func<TLocal>, Func<Int32,ParallelLoopState,TLocal,TLocal>, Action<TLocal>) |
执行具有线程本地数据的 |
For<TLocal>(Int64, Int64, Func<TLocal>, Func<Int64,ParallelLoopState,TLocal,TLocal>, Action<TLocal>) |
执行具有 64 位索引和线程本地数据的 |
For<TLocal>(Int64, Int64, ParallelOptions, Func<TLocal>, Func<Int64,ParallelLoopState,TLocal,TLocal>, Action<TLocal>) |
执行具有 64 位索引和线程本地数据的 |
ForEach<TSource,TLocal>(IEnumerable<TSource>, Func<TLocal>, Func<TSource,ParallelLoopState,Int64,TLocal,TLocal>, Action<TLocal>) |
执行具有线程本地数据的 |
ForEach<TSource,TLocal>(IEnumerable<TSource>, Func<TLocal>, Func<TSource,ParallelLoopState,TLocal,TLocal>, Action<TLocal>) |
执行具有线程本地数据的 |
ForEach<TSource,TLocal>(IEnumerable<TSource>, ParallelOptions, Func<TLocal>, Func<TSource,ParallelLoopState,Int64,TLocal,TLocal>, Action<TLocal>) |
执行具有线程本地数据和 64 位索引的 |
ForEach<TSource,TLocal>(IEnumerable<TSource>, ParallelOptions, Func<TLocal>, Func<TSource,ParallelLoopState,TLocal,TLocal>, Action<TLocal>) |
执行具有线程本地数据的 |
ForEach<TSource,TLocal>(OrderablePartitioner<TSource>, Func<TLocal>, Func<TSource,ParallelLoopState,Int64,TLocal,TLocal>, Action<TLocal>) |
执行具有线程本地数据的 |
ForEach<TSource,TLocal>(OrderablePartitioner<TSource>, ParallelOptions, Func<TLocal>, Func<TSource,ParallelLoopState,Int64,TLocal,TLocal>, Action<TLocal>) |
执行具有 64 位索引和线程本地数据的 |
ForEach<TSource,TLocal>(Partitioner<TSource>, Func<TLocal>, Func<TSource,ParallelLoopState,TLocal,TLocal>, Action<TLocal>) |
执行具有线程本地数据的 |
ForEach<TSource,TLocal>(Partitioner<TSource>, ParallelOptions, Func<TLocal>, Func<TSource,ParallelLoopState,TLocal,TLocal>, Action<TLocal>) |
执行具有线程本地数据的 |
ForEach<TSource>(IEnumerable<TSource>, Action<TSource,ParallelLoopState,Int64>) |
执行具有 64 位索引的 |
ForEach<TSource>(IEnumerable<TSource>, Action<TSource,ParallelLoopState>) |
执行 |
ForEach<TSource>(IEnumerable<TSource>, Action<TSource>) |
执行 |
ForEach<TSource>(IEnumerable<TSource>, ParallelOptions, Action<TSource,ParallelLoopState,Int64>) |
执行具有 64 位索引的 |
ForEach<TSource>(IEnumerable<TSource>, ParallelOptions, Action<TSource,ParallelLoopState>) |
执行 |
ForEach<TSource>(IEnumerable<TSource>, ParallelOptions, Action<TSource>) |
执行 |
ForEach<TSource>(OrderablePartitioner<TSource>, Action<TSource,ParallelLoopState,Int64>) |
执行 |
ForEach<TSource>(OrderablePartitioner<TSource>, ParallelOptions, Action<TSource,ParallelLoopState,Int64>) |
执行 |
ForEach<TSource>(Partitioner<TSource>, Action<TSource,ParallelLoopState>) |
执行 |
ForEach<TSource>(Partitioner<TSource>, Action<TSource>) |
执行 |
ForEach<TSource>(Partitioner<TSource>, ParallelOptions, Action<TSource,ParallelLoopState>) |
执行 |
ForEach<TSource>(Partitioner<TSource>, ParallelOptions, Action<TSource>) |
执行 |
ForEachAsync<TSource>(IAsyncEnumerable<TSource>, CancellationToken, Func<TSource,CancellationToken,ValueTask>) |
对迭代并行运行 |
ForEachAsync<TSource>(IAsyncEnumerable<TSource>, Func<TSource,CancellationToken,ValueTask>) |
对迭代并行运行 |
ForEachAsync<TSource>(IAsyncEnumerable<TSource>, ParallelOptions, Func<TSource,CancellationToken,ValueTask>) |
对迭代并行运行 |
ForEachAsync<TSource>(IEnumerable<TSource>, CancellationToken, Func<TSource,CancellationToken,ValueTask>) |
对迭代并行运行 |
ForEachAsync<TSource>(IEnumerable<TSource>, Func<TSource,CancellationToken,ValueTask>) |
对迭代并行运行 |
ForEachAsync<TSource>(IEnumerable<TSource>, ParallelOptions, Func<TSource,CancellationToken,ValueTask>) |
对迭代并行运行 |
Invoke(Action[]) |
尽可能并行执行提供的每个操作。 |
Invoke(ParallelOptions, Action[]) |
执行所提供的每个操作,而且尽可能并行运行,除非用户取消了操作。 |
常用的几个方法:
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); } }View Code
这篇关于C# Task.Parallel的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#