Pass Parameter to Thread in C#

«
»

This is a C# Program to illustrate the concept of passing parameter for thread.

Problem Description

This C# Program Illustrates the Concept of Passing Parameter for Thread.

Problem Solution

Here the following code example shows the syntax for creating and using a ParameterizedThreadStart delegate with a static method and an instance method.

Program/Source Code

Here is source code of the C# Program to Illustrate the Concept of Passing Parameter for Thread. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Illustrate the Concept of Passing Parameter for Thread
 */
using System;
using System.Threading;
public class pgm
{
    public static void Main()
    {
        Thread newThread = new Thread(pgm.work1);
        newThread.Start(20);
        pgm p = new pgm();
        newThread = new Thread(p.work2);
        newThread.Start("Instance");
        Console.ReadLine();
    }
    public static void work1(object data)
    {
        Console.WriteLine("Static Thread Procedure : Data ='{0}'",data);
    }
    public void work2(object data)
    {
        Console.WriteLine("Instance Thread Procedure : Data ='{0}'", data);
    }
}
Program Explanation

In this C# Program, we are creating a new thread object variable for thread constructor. Assign the starting time as 20, and create the object variable ‘p’. For that variable again create the new thread and make it has an instance.

advertisement
advertisement
Runtime Test Cases
 
Static Thread Procedure : Data = '20'
Instance Thread Procedure : Data = 'Instance'

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.