C# | Thread(ParameterizedThreadStart) Constructor
Last Updated :
14 Sep, 2021
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.
Syntax:
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:
CSharp
using System;
using System.Threading;
public class MYTHREAD {
public void Job()
{
for ( int z = 0; z < 3; z++) {
Console.WriteLine( "My thread is " +
"in progress...!!" );
}
}
}
public class GFG {
public static void Main()
{
MYTHREAD obj = new MYTHREAD();
Thread thr = new Thread(obj.Job);
thr.Start();
}
}
|
Output:
My thread is in progress...!!
My thread is in progress...!!
My thread is in progress...!!
Example 2:
CSharp
using System;
using System.Threading;
public class GFG {
public static void Main()
{
Thread thr = new Thread(Job);
thr.Start();
}
public static void Job()
{
Console.WriteLine( "My thread is" +
" in progress...!!" );
for ( int z = 0; z < 3; z++) {
Console.WriteLine(z);
}
}
}
|
Output:
My thread is in progress...!!
0
1
2
Reference:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...