Skip to content
Related Articles

Related Articles

C# | Thread(ParameterizedThreadStart) Constructor
  • Last Updated : 01 Feb, 2019

Thread(ParameterizedThreadStart) Constructor is used to initialize a new instance of the Thread class. It defined a delegate which allows an object to pass to the thread when the thread starts. This constructor gives ArgumentNullException if the parameter of this constructor is null.

Synatx:

public Thread(ParameterizedThreadStart start);

Here, start is a delegate which represents a method to be invoked when this thread begins executing.

Below programs illustrate the use of Thread(ParameterizedThreadStart) Constructor:

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the
// use of Thread(ParameterizedThreadStart)
// constructor with non-static method
using System;
using System.Threading;
  
public class MYTHREAD {
  
    // Non-static method
    public void Job()
    {
        for (int z = 0; z < 3; z++) {
  
            Console.WriteLine("My thread is "+
                          "in progress...!!");
        }
    }
}
  
// Driver Class
public class GFG {
  
    // Main Method
    public static void Main()
    {
        // Creating object of MYTHREAD class
        MYTHREAD obj = new MYTHREAD();
  
        // Creating a thread which
        // calls a parameterized instance method
        Thread thr = new Thread(obj.Job);
        thr.Start();
    }
}

chevron_right


Output:

My thread is in progress...!!
My thread is in progress...!!
My thread is in progress...!!

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the use of
// Thread(ParameterizedThreadStart)
// constructor with static method
using System;
using System.Threading;
  
// Driver Class
public class GFG {
  
    // Main Method
    public static void Main()
    {
        // Creating a thread which calls 
        // a parameterized static-method
        Thread thr = new Thread(Job);
        thr.Start();
    }
  
    // Static method
    public static void Job()
    {
        Console.WriteLine("My thread is"+
                    " in progress...!!");
  
        for (int z = 0; z < 3; z++) {
            Console.WriteLine(z);
        }
    }
}

chevron_right


Output:

My thread is in progress...!!
0
1
2

Reference:

My Personal Notes arrow_drop_up
Recommended Articles
Page :