Parallel use of Parallel. Invoke,
The Parallel class is in the namespace of System. Threading. Tasks.
There are several methods below. Here we will talk about the usage of Invoke.
Below we define several methods to facilitate testing
First customize Response to prevent the result changes caused by occupying in parallel
HttpResponse MyResponse = System. Web. HttpContext. Current. Response;
public void ResponseWrite1() { string str = "1:"; for (int i = 0; i < 10; i++) { System.Threading.Thread.Sleep(15); str += i; } lock (MyResponse) { MyResponse.Write(str + "<br /><br />"); } } public void ResponseWrite2() { string str = "2:"; for (int i = 0; i < 10; i++) { System.Threading.Thread.Sleep(15); str += i; } lock (MyResponse) { MyResponse.Write(str + "<br /><br />"); } } public void ResponseWrite3() { string str = "3:"; for (int i = 0; i < 10; i++) { System.Threading.Thread.Sleep(15); str += i; } lock (MyResponse) { MyResponse.Write(str + "<br /><br />"); } }
Next we will start using parallel
You can run methods without parameters in parallel in the following way (☆☆invoke can only pass in method names)
System. diagnostics. stopwatch watch = new System. diagnostics. stopwatch (); watch. start (); ResponseWrite1 (); ResponseWrite2 (); ResponseWrite3 (); watch. stop (); MyResponse. write ($ "<br/> Common Serial Time: {watch. elapsed. milliseconds} millisecond <br/> "); System. diagnostics. stopwatch watch2 = new System. diagnostics. stopwatch (); watch2.Start (); Parallel. invoke (ResponseWrite1, ResponseWrite2, ResponseWrite3); watch2.Stop (); MyResponse. write ($ "<br/> parallel time consumption: {watch2.Elapsed. milliseconds} millisecond <br/> ");
The execution result is as follows: (☆☆☆note that the order of Invoke execution is not fixed)
We can see that executing the same code in parallel is obviously more time-saving.
Can we execute a method with parameters?
The answer is yes, of course.
We can execute it using Lambda (or delegate)
First, add a parameter to the test method.
public void ResponseWrite1(string param1 = "test") { string str = param1 + "1:"; for (int i = 0; i < 10; i++) { System.Threading.Thread.Sleep(15); str += i; } lock (MyResponse) { MyResponse.Write(str + "<br /><br />"); } }
Use lambda to execute a method with Parameters
System. Diagnostics. Stopwatch watch = new System. Diagnostics. Stopwatch ();
Watch. start (); ResponseWrite1 (); ResponseWrite2 (); ResponseWrite3 (); watch. stop (); MyResponse. write ($ "<br/> Common Serial Time: {watch. elapsed. milliseconds} millisecond <br/> "); System. diagnostics. stopwatch watch2 = new System. diagnostics. stopwatch (); watch2.Start (); Parallel. invoke () => ResponseWrite1 ("HAHAHA"), () => ResponseWrite2 (), delegate (){
ResponseWrite3 ();
}
); Watch2.Stop (); MyResponse. Write ($ "<br/> parallel time consumption: {watch2.Elapsed. Milliseconds} millisecond <br/> ");
The Parallel. Invoke method is returned only after all methods are executed. Even if an exception occurs during method execution, the Invoke is completed.
So we are using Parallel. when Invoke is used, check whether the execution time of the method is similar. If one method runs for a long time, it will also drag other methods (because Invoke will not return until all methods are executed)
The above is the usage of Parallel. Invoke. If there is something wrong or can be improved, please leave a message