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