SoFunction
Updated on 2025-03-07

C# method to create thread with parameters

1. Creation of no parameter threads

Thread thread = new Thread(new ThreadStart(getpic));
();
private void showmessage()
{
("hello world");
}

2. Thread with one parameter

Using ParameterizedThreadStart, the object containing the data is passed to the thread when the () overload method is called.

Note that the passed parameters can only be of object type, but casting can be performed.

Thread thread = new Thread(new ParameterizedThreadStart(showmessage));
string o = "hello";
((object)o);
private static void showmessage(object message)
{
string temp = (string)message;
(message);
}

3. Threads with two or more parameters

At this time, the methods and parameters executed by the thread can be encapsulated into a class. By instantiating the class, the method can call attributes to enjoy the passing parameters.

For example, the following program wants to pass in two string variables and then print the output.

public class ThreadTest
{
private string str1;
private string str2;
public ThreadTest(string a, string b)
{
str1 = a;
str2 = b;
}
public void ThreadProc()
{
(str1 + str2);
}
}
public class Example {
public static void Main() 
{
ThreadTest tt = new ThreadTest("hello ", "world");
Thread thread = new Thread(new ThreadStart());
();
}
}

The above is the method of creating C# threads with parameters introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!