To work with threads, add the following namespace in your code −
using System.Threading;
Firstly, you need to create a new thread in C# −
Thread thread = new Thread(threadDemo);
Above, threadDemo is our thread function.
Now pass a parameter to the thread −
thread.Start(str);
The parameter set above is −
String str = "Hello World!";
Example
Let us see the complete code to pass a parameter to a thread in C#.
using System;
using System.Threading;
namespace Sample {
class Demo {
static void Main(string[] args) {
String str = "Hello World!";
// new thread
Thread thread = new Thread(threadDemo);
// passing parameter
thread.Start(str);
}
static void threadDemo(object str) {
Console.WriteLine("Value passed to the thread: "+str);
}
}
}Output
Value passed to the thread: Hello World!