Open In App

C# | Thread(ThreadStart) Constructor

Improve
Improve
Like Article
Like
Save
Share
Report

Thread(ThreadStart) Constructor is used to initialize a new instance of a Thread class. This constructor will give ArgumentNullException if the value of the parameter is null.

Syntax:

public Thread(ThreadStart start);

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

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

Example 1:




// C# program to illustrate the
// use of Thread(ThreadStart) 
// constructor with static method
using System;
using System.Threading;
  
// Driver Class
class GFG {
  
    // Main Method
    public static void Main()
    {
  
        // Creating and initializing a thread
        // with Thread(ThreadStart) constructor
        Thread thr = new Thread(new ThreadStart(Job));
        thr.Start();
    }
  
    // Static method
    public static void Job()
    {
        Console.WriteLine("Number is :");
        for (int z = 0; z < 4; z++) {
            Console.WriteLine(z);
        }
    }
}


Output:

Number is :
0
1
2
3

Example 2:




// C# program to illustrate the
// use of Thread(ThreadStart) 
// constructor with Non-static method
using System;
using System.Threading;
  
class GThread {
  
    // Non-static method
    public void Job()
    {
        for (int z = 0; z < 3; z++) {
            Console.WriteLine("HELLO...!!");
        }
    }
}
  
// Driver Class
public class GFG {
  
    // Main Method
    public static void Main()
    {
        // Creating object of GThread class
        GThread obj = new GThread();
  
        // Creating and initializing a thread
        // with Thread(ThreadStart) constructor
        Thread thr = new Thread(new ThreadStart(obj.Job));
        thr.Start();
    }
}


Output:

HELLO...!!
HELLO...!!
HELLO...!!

Reference:



Last Updated : 01 Feb, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads