Pass multiple parameters to multiple threads in C#

1. Method 1: Use ParameterizedThreadStart delegate
If a delegate ParameterizedThreadStart, entry of the thread must have a type of object parameters, and return type void.
View Code
the using the System;
the using the System.Threading;

namespace ThreadWithParameters
{
class Program
{
static void Main(string[] args)
{
string hello = “hello world”;

         //这里也可简写成Thread thread = new Thread(ThreadMainWithParameters);
         //但是为了让大家知道这里用的是ParameterizedThreadStart委托,就没有简写了
         Thread thread = new Thread(new ParameterizedThreadStart(ThreadMainWithParameters));
         thread.Start(hello);

         Console.Read();
     }

     static void ThreadMainWithParameters(object obj)
     {
         string str = obj as string;
         if(!string.IsNullOrEmpty(str))
             Console.WriteLine("Running in a thread,received: {0}", str);
     }
 }

}The
above code can only pass one parameter. If sometimes we pass multiple parameters to the thread, the use of that way will have limitations (if a class is used as an object to pass parameters, then another class needs to be created, which is also a little troublesome)
If we use this method, we should pay close attention to that the parameters in the ThreadMainWithParameters method must be of object type, and we need to perform type conversion. This method is not recommended.
2. Second way: create a custom class
definition of a class, which defines the fields required in the method of the main thread class definition of an instance method, see practical example.
View Code
using System;
using System.Threading;

namespace ThreadWithParameters
{
public class MyThread
{
private string data;

     public MyThread(string data)
     {
         this.data = data;
     }

     public void ThreadMain()
     {
         Console.WriteLine("Running in a thread,data: {0}", data);
     }
 }

 class Program
 {
     static void Main(string[] args)
     {
         MyThread myThread = new MyThread("hello world");

         Thread thread = new Thread(myThread.ThreadMain);
         thread.Start();

         Console.Read();
     }
 }

}
This method is also a bit cumbersome, and it is not what I want to focus on. If necessary, I can use it myself.
3. Three ways: using lambda expression
for lambda expressions are not familiar with can view the documentation on the Microsoft MSDN. It is assumed that you are familiar here. Because we can generally use lambda expressions when using delegates.
View Code
using System;
using System.Threading;

namespace ThreadWithParameters
{
class Program
{
static void Main(string[] args)
{
string hello = “hello world”;

         //如果写成Thread thread = new Thread(ThreadMainWithParameters(hello));这种形式,编译时就会报错
         Thread thread = new Thread(() => ThreadMainWithParameters(hello));
         thread.Start();

         Console.Read();
     }

     static void ThreadMainWithParameters(string str)
     {
          Console.WriteLine("Running in a thread,received: {0}", str);
     }
 }

}
This method three can be used as a recommended method, the code is simple and convenient for us to use. The third method above talks about lambda expressions. Since lambda expressions can be used, we can also use delegate. The main discussion below is to use this method.
4. Method 3: Use delegate to delegate
Multhd = new Thread[ThreadCount];
thread_Separate_baseCount = C_num / ThreadCount;//The number of terminals per thread
listViewEx2.Items.Clear();
for (int j = 0; j <ThreadCount; j++ )
{
if (j == ThreadCount-1)//The last thread
{
Multhd[j] = new Thread(delegate() {Run2( j * thread_Separate_baseCount, C_num-1); });
}
else//Other threads
{
Multhd[j] = new Thread(delegate() {Run2( j * thread_Separate_baseCount, (j + 1) * thread_Separate_baseCount-1); });
}
} The
above code Run(arg1,arg2.....,argn) is our self The defined method, in this method, we can set any number of parameters, it can be considered simple.
The above code can run perfectly, and there will be no bugs or the like, but the actual running situation has surprised me. When a thread is created with a for loop, there will be no problem with closing and restarting the thread. Because it is just a thread, there may be nothing wrong. But when there are two or more, close and reopen the thread (I need to keep closing and opening the terminal), the program will not report an error, the debugger will have no problem, and the computer will sometimes shut down without warning, when we are doing things. Shutting down without warning is a fatal problem, and no one wants to. I thought it was accidental, and the result was the same after repeated many times, and accidental inevitability must exist.
Why is one thread okay? When multiple threads are running, are the threads above all pointing to the address of the same delegate, which causes the shutdown without warning? These are waiting for us to verify.
Reprinting place: https://www.cnblogs.com/lvdongjie/p/5416883.html

Guess you like

Origin blog.csdn.net/hello_mr_anan/article/details/82114708