The basic usage of thread class
The System.Threading.Thread class allows you to start a new thread and run static or instance methods in the thread stack. A delegate (ThreadStart) that has no parameters and does not return a value (return void) can be passed through the construction method of the thread class, which is defined as follows:
[ComVisibleAttribute(true)]
public delegate void ThreadStart()
We can build and run a thread in the following ways.
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading;
Namespace Mythread
{
Class Program
{
public static void Mystaticthreadmethod ()
{
Console.WriteLine ("Mystaticthreadmethod");
}
static void Main (string[] args)
{
Thread thread1 = new Thread (Mystaticthreadmethod);
Thread1. Start (); The thread will run only if you use the Start method
}
}
}
In addition to running a static method, you can run an instance method in a thread, as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading;
Namespace Mythread
{
Class Program
{
public void Mythreadmethod ()
{
Console.WriteLine ("Mythreadmethod");
}
static void Main (string[] args)
{
Thread thread2 = new Thread (new program (). Mythreadmethod);
Thread2. Start ();
}
}
}