Create thread instance tutorial using ES0en.NET

  • 2021-01-03 20:52:05
  • OfStack

The implementation of creating a thread using ASP.NET is as simple as declaring it and providing it with a method delegate at the thread's starting point. When you create a new thread, you need to use the Thread class, which has a constructor that accepts either an ThreadStart delegate or an ParameterizedThreadStart delegate. This delegate wraps the method called by the new thread when the Start method is called. After the object of the Thread class is created, the thread object is already there and configured, but the actual thread is not created, and the actual thread is created only after the Start method is called.

The Start method of ES12en. NET, which allows threads to be scheduled for execution, has two types of overloads, described below.

(1) Causes the operating system to change the state of the current instance to ES17en.Running, with the following syntax.


public void Start ()

(2) Cause the operating system to change the state of the current instance to ThreadState.Running and select the object that provides the data to be used by the method executed by the thread. The syntax is as follows.


public void Start (Object parameter)

parameter: 1 object containing the data to be used by methods executed by the thread.

Note: If the thread has terminated, it cannot be restarted by calling the Start method again.

For example: create a console application with a custom static void type method createThread, then create a new thread in the Main method by instantiating the Thread class object, and then call the Start method to start the thread. The specific code is as follows:


static void Main(string[] args)
{
  Thread myThread; // Announcement thread 
// With a thread starting point ThreadStart The delegate creates an instance of the thread 
myThread = new Thread(new ThreadStart(createThread));
  myThread.Start();// Starting a thread 
}
public static void createThread()
{
  Console.Write(" Create a thread ");
}

Note: The entry to the thread (createThread in this case) takes no arguments.


Related articles: