Passing Data to a Thread

The easiest way to pass arguments to a thread’s target method is to execute a lambda expression that calls the method with the desired arguments:

Example

using System;
using System.Threading;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            Thread t = new Thread(();
            t.Start();
            Console.Read();
        }
        static void Print(string message)
        {
            Console.WriteLine(message);
        }
    }
}

Another technique is to pass an argument into Thread’s Start method:

Example

using System;
using System.Threading;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            Thread t = new Thread(Print);
            t.Start("Hello from t!");
            Console.Read();
        }
        static void Print(object messageObj)
        {
            string message = (string)messageObj; // We need to cast here
            Console.WriteLine(message);
        }
    }
}