Thread Everyone has heard, also more or less seen or used. But for the parameter passing in the thread may be unfamiliar, today I am here to share with you the method of passing the thread.
Let's take a look at the thread without the parameter before sharing the thread's parameter passing method:
Using System.Threading; (Don't forget to add namespaces before using threads)
public class ThreadClass
{
public static void GetThread ()
{
Thread thread=new thread (t),//thread thread=new thread (new ThreadStart (t));//Two sentences have the same effect
Thread.Start ();
}
public string T ()
{
return 0;
}
}
The above code is to use a thread in the ThreadClass class with a getthread () function, and I don't know if you have noticed the comment in the first line of the function in the class, there is a threadstart in the comment;
The ThreadStart master is used to define threads without parameters in the thread, which is also a method we often use.
Let's take a look at the threads with parameter passing:
Using System.Threading; (Don't forget to add namespaces before using threads)
public class ThreadClass
{
public static void GetThread ()
{
Thread Thread=new Thread (new Parameterizedthreadstart (T));
Thread.Start (10);
}
public string T (object i)
{
String str=i.tostring ();
return str;
}
}
Let's compare the difference between using a thread with no parameters:
1. Threads without parameters are defined using ThreadStart, whereas threads with parameter passing are defined with Parameterizedthreadstart;
2. The method called by the thread is one with no parameters and the other with an object parameter;
3. When the thread is enabled (Start ()), the thread in which the parameter is passed is parameter-free, without the parameter.
From these differences we can make a summary, use ThreadStart to define threads when using a thread without parameters, the thread with parameter passing is defined with Parameterizedthreadstart, and the thread is passed with start when it is passed.
Now let's take a look at the next parameterizdethreadstart, in a thread defined with Parameterizedthreadstart, the thread can accept an object parameter, but must have one in more. ; So the method called in the thread with parameters has only one object parameter.
Someone here might ask, if I'm going to pass two or two more than what should be done, the threading method with parameter passing can only pass one parameter; Here I give you a way to pass two parameters, the code is as follows:
Using System.Threading; (Don't forget to add namespaces before using threads)
public class ThreadClass
{
public static void GetThread ()
{
Thread Thread=new Thread (new Parameterizedthreadstart (T));
Thread.Start (10);
}
public string T (object i)
{
String str=i.tostring ();
String b= "Hello";
String S=t2 (STR,B);
return S;
}
public string T2 (String i,string j)
{
String s=i+ "," +j+ "";
return s;
}
}
Parameter passing of a thread