There is often a need of passing data from the main thread of a program to a worker threads. The Thread.Start method has an overloaded form that allows code to pass an object from main thread to a new thread. The object can be a simple data type or it can be a complex data type.
The Thread class constructor takes either a ThreadStart delegate or a ParemeterizedThreadStart delegate. The ParemeterizedThreadStart delegate is used when you need to pass data to the thread.
Let’s look at the code snippet in Listing 10 where the Print class has two methods, PrintJob and PrintPerson. The PrintJob method simply prints data passed to it. The PrintPerson expect the data to be a type of Person. The method reads the Person object details and prints them.
We want to execute these methods in two separate worker threads. As you can clearly see, both methods take a parameter of object type.
- public class Print
- {
- public void PrintJob(object data)
- {
- Console.WriteLine("Background PrintJob started.");
- Thread.Sleep(1000);
- Console.WriteLine("PrintJob still printing...");
- Console.WriteLine($"Data: {data}");
- Thread.Sleep(1000);
- Console.WriteLine("PrintJob finished.");
- }
-
- public void PrintPerson(object data)
- {
- Console.WriteLine("Background PrintPerson started.");
- Thread.Sleep(1000);
- Console.WriteLine("PrintPerson still printing...");
- Person p = (Person)data;
- Console.WriteLine($"Person {p.Name} is a {p.Sex} of {p.Age} age.");
- Thread.Sleep(1000);
- Console.WriteLine("PrintPerson finished.");
- }
- }
Listing 10.
Now, we will see how to pass an object type from the main thread to a worker thread.
The code snippet in Listing 11 starts a new worker thread that executes the PrintJob method. Since the PrintJob is a ParemeterizedThreadStart delegate, the Start method takes a parameter. In this case, I pass a string as a parameter.
-
- Print p = new Print();
- Thread workerThread = new Thread(p.PrintJob);
-
- workerThread.Start("Some data");
Listing 11.
The PrintPerson method of the Print class takes a complex object of type Person. In code snippet in Listing 12, I create a Person class and pass it as a parameter of the Thread.Start method.
-
- Person mahesh = new Person("Mahesh Chand", 40, "Male");
- Thread workerThread2 = new Thread(p.PrintPerson);
- workerThread2.Start(mahesh);
Listing 12.
The ParameterizedThreadStart delegate supports only a single parameter. To pass complex or multiple data items, you can pass an Array, a collection type, or a tuple type.
Listing 13 is a complete code listing.
- using System;
- using System.Threading;
- class Program
- {
- static void Main()
- {
-
- Print p = new Print();
- Thread workerThread = new Thread(p.PrintJob);
-
- workerThread.Start("Some data");
-
-
- Person mahesh = new Person("Mahesh Chand", 40, "Male");
- Thread workerThread2 = new Thread(p.PrintPerson);
- workerThread2.Start(mahesh);
-
-
- for (int i = 0; i < 10; i++)
- {
- Console.WriteLine($"Main thread: {i}");
- Thread.Sleep(200);
- }
-
- Console.ReadKey();
- }
- }
-
- public class Print
- {
- public void PrintJob(object data)
- {
- Console.WriteLine("Background PrintJob started.");
- Thread.Sleep(1000);
- Console.WriteLine("PrintJob still printing...");
- Console.WriteLine($"Data: {data}");
- Thread.Sleep(1000);
- Console.WriteLine("PrintJob finished.");
- }
-
- public void PrintPerson(object data)
- {
- Console.WriteLine("Background PrintPerson started.");
- Thread.Sleep(1000);
- Console.WriteLine("PrintPerson still printing...");
- Person p = (Person)data;
- Console.WriteLine($"Person {p.Name} is a {p.Sex} of {p.Age} age.");
- Thread.Sleep(1000);
- Console.WriteLine("PrintPerson finished.");
- }
- }
-
- public class Person
- {
- public string Name { get; set; }
- public int Age { get; set; }
- public string Sex { get; set; }
-
- public Person(string name, int age, string sex)
- {
- this.Name = name;
- this.Age = age;
- this.Sex = sex;
- }
- }
Listing 13.
Summary
In this article, I demonstrated how to use the ThreadStart to pass data from your main program to a new thread using the ParameterizedThreadStart.
A managed thread pool is a recommended method of using system managed threads. Check out my article,
Managed Thread Pool In C# for more details.