.net Parallel 介绍 电脑版发表于:2023/11/12 22:04 可以并行执行,代码如下: ``` public static void ParallelInvoke() { Stopwatch sp = new Stopwatch(); sp.Start(); Run1(); Run2(); sp.Stop(); Console.WriteLine("Normal run " + sp.ElapsedMilliseconds + " ms."); sp.Restart(); Parallel.Invoke(Run1, Run2); sp.Stop(); Console.WriteLine("Parallel run " + sp.ElapsedMilliseconds + " ms."); } public static void Run1() { Thread.Sleep(2000); Console.WriteLine("I am Run1"); } public static void Run2() { Thread.Sleep(3000); Console.WriteLine("I am Run2"); } ```