C# | Thread(ThreadStart) Constructor
Last Updated :
01 Feb, 2019
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:
using System;
using System.Threading;
class GFG {
public static void Main()
{
Thread thr = new Thread( new ThreadStart(Job));
thr.Start();
}
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:
using System;
using System.Threading;
class GThread {
public void Job()
{
for ( int z = 0; z < 3; z++) {
Console.WriteLine( "HELLO...!!" );
}
}
}
public class GFG {
public static void Main()
{
GThread obj = new GThread();
Thread thr = new Thread( new ThreadStart(obj.Job));
thr.Start();
}
}
|
Output:
HELLO...!!
HELLO...!!
HELLO...!!
Reference:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...