Parallel use of Parallel. Invoke,

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.