ADVERTISEMENT
ADVERTISEMENT

C# program to demonstrate the concept of parameter passing for thread

Parameter passing in a thread in C#: Here, we are going to learn the concept of parameter passing for thread in C#?
Submitted by Nidhi, on August 16, 2020

To solve the above problem, here we demonstrate the parameter passing in the static and instance thread methods.

Program:

/*
 * Program to demonstrate the concept of 
 * parameter passing for thread in C#.
 */
using System;
using System.Threading;

public class MyThreadClass
{
    public static void StaticThreadMethod(object param)
    {
        Console.WriteLine("StaticThreadMethod:param->"+param);
    }
    public void InstanceThreadMethod(object param)
    {
        Console.WriteLine("InstanceThreadMethod:param->"+param);
    }

    public static void Main()
    {
        Thread T = new Thread(MyThreadClass.StaticThreadMethod);
        T.Start(100);
        
        MyThreadClass p = new MyThreadClass();
        T = new Thread(p.InstanceThreadMethod);
        T.Start(200);

    }    
}

Output:

StaticThreadMethod:param->100
InstanceThreadMethod:param->200
Press any key to continue . . .

Explanation:

In the above program, we created a class MyThreadClass that contains one static method StaticThreadMethod() and an instance method InstanceThreadMethod. Both method accept the argument of object type and print then on the console screen. The MyThreadClass also contains a Main() method.

In the Main() method we created a thread T.

Thread T = new Thread(MyThreadClass.StaticThreadMethod);
T.Start(100);

Here we bind the StaticThreadethod() with thread T and pass value 100 as a parameter.

MyThreadClass p = new MyThreadClass();
T = new Thread(p.InstanceThreadMethod);
T.Start(200);

Here we bind the InstanceThreadethod() with thread T and pass value 200 as a parameter.

C# Thread Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.