Multi-threaded Way with parameters

Source: Internet
Author: User

This article from: http://www.cnblogs.com/zeroone/archive/2013/09/04/3300274.html

The #region executes a multithreaded thread with one parameter            mythread = new Thread (new Parameterizedthreadstart (Calculate));            Mythread. IsBackground = true;                   Mythread. Start (+);            #endregion        private void Calculate (object Max)              //delegate function with one parameter          {            int max = (int) max;            Stopwatch Stopwatch = Stopwatch.startnew ();            for (int i = 0; i < max; i++)            {                thread.sleep (5);            }            Stopwatch. Stop ();            Long lsearchtime = stopwatch. Elapsedmilliseconds;            MessageBox.Show (lsearchtime.tostring () + "MS");        }



Method One: Define a class, set the parameters to be passed to the properties of the class, and then assign the parameter value to the class's properties, the class as a parameter to communicate, the following code through two parameter examples, the same as several parameters, the code is as follows

    Class MyClass    {public        int Max {get; set;}        public int Min {get; set;}    }            #region The first way: Execute multithreaded            MyClass model = new MyClass () with multiple parameters;            Model. Max =;            Model. Min = 0;            Thread mythread1 = new Thread (new Parameterizedthreadstart (Calculatetwo));            Mythread1. IsBackground = true;                   Mythread1. Start (model);            #endregion        private void Calculatetwo (object Myclass)              //delegate function with multiple arguments          {            Myclass model = (Myclass) Myclass;            Stopwatch Stopwatch = Stopwatch.startnew ();            for (int i = model. Min; I < model. Max; i++)            {                thread.sleep (5);            }            Stopwatch. Stop ();            Long lsearchtime = stopwatch. Elapsedmilliseconds;            MessageBox.Show (lsearchtime.tostring () + "MS");        }

Mode two: lambda expression, simple and convenient, the code is as follows:



#region The second way: Execute multithreaded thread mythread2 with multiple parameters = new Thread (() = Calculatethree (0)); Mythread2. IsBackground = true; Set to the back thread, the program is closed after the process is also closed, if not set true, the program is closed, this line have in the memory, will not close mythread2. Start (); #endregion private void calculatethree (int max,int Min) //delegate function with multiple arguments { Stopwatch Stopwatch = Stopwatch.startnew (); for (int i = Min; i < Max; i++) { thread.sleep (5); } Stopwatch. Stop (); Long lsearchtime = stopwatch. Elapsedmilliseconds; MessageBox.Show (lsearchtime.tostring () + "MS"); }
Tags: C #, multithreading

Multi-threaded Way with parameters

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.