Next we will create a Thread. When using the Thread class to create a Thread, we only need to provide the Thread entry. (The thread entry tells the program what to do with this thread)
In C #, the Thread entry is provided through the ThreadStart proxy (delegate). You can regard ThreadStart as a function pointer pointing to the function to be executed by the Thread. When Thread is called. after the Start () method, the thread starts to execute the Functions Represented or pointed to by ThreadStart.
Open your VS.net, create a Console Application, and write a sample code that completely controls a thread:
Using System;
Using System. Threading;
Namespace ThreadTest
{
Public class Alpha
{
Public void Beta ()
{
While (true)
{
Console. WriteLine ("Alpha. Beta is running in its own thread .");
}
}
};
Public class Simple
{
Public static int Main ()
{
Console. WriteLine ("Thread Start/Stop/Join Sample ");
Alpha oAlpha = new Alpha ();
File: // create a thread to execute the Beta () method of the Alpha Class.
Thread oThread = new Thread (new ThreadStart (oAlpha. Beta ));
OThread. Start ();
While (! OThread. IsAlive)
Thread. Sleep (1 );
OThread. Abort ();
OThread. Join ();
Console. WriteLine ();
Console. WriteLine ("Alpha. Beta has finished ");
Try
{
Console. WriteLine ("Try to restart the Alpha. Beta thread ");
OThread. Start ();
}
Catch (ThreadStateException)
{
Console. Write ("ThreadStateException trying to restart Alpha. Beta .");
Console. WriteLine ("Expected since aborted threads cannot be restarted .");
Console. ReadLine ();
}
Return 0;
}
}
}
This program contains two classes: Alpha and Simple. when creating the thread oThread, we use the pointer to Alpha. the ThreadStart proxy (delegate) object is initialized in the Beta () method. When the created thread oThread calls oThread. when the Start () method is started, the actual program running is Alpha. beta () method:
Alpha oAlpha = new Alpha ();
Thread oThread = new Thread (new ThreadStart (oAlpha. Beta ));
OThread. Start ();
Then in the while loop of the Main () function, we use the static method Thread. Sleep () to stop the Main Thread for 1 ms. During this time, the CPU turns to the execution Thread oThread. Then we try to use the Thread. Abort () method to terminate the Thread oThread. Pay attention to the following oThread. Join (), Thread. Join () method to wait for the main Thread until the oThread Thread ends. You can specify an int-type parameter for the Thread. Join () method as the maximum waiting time. Later, we tried to use the Thread. Start () method to restart the Thread oThread, but obviously the consequence of the Abort () method is that the Thread cannot be recovered, so the program will throw a ThreadStateException.
Main () function of the Main thread
All threads are dependent on the thread where the Main () function is located. The Main () function is the entry of the C # program. The starting thread can be called the Main thread. If all foreground threads are stopped, the main thread can be terminated, and all background threads will be terminated unconditionally. Although all threads are executed in a serial way at a micro level, you can think of them as being executed in parallel at a macro level.
Thread. ThreadState attributes
This attribute represents the running state of the thread and has different values under different circumstances. Sometimes we can design the program flow by judging the value.
The value of the ThreadState attribute is as follows:
Aborted: the thread has stopped;
AbortRequested: The Thread. Abort () method of the Thread has been called, but the Thread has not stopped;
Background: The Thread is executed in the Background, which is related to the attribute Thread. IsBackground;
Running: The thread is Running normally;
Stopped: the thread has been Stopped;
StopRequested: The thread is being requested to stop;
Suincluded: the thread has been Suspended. (In this status, you can call Resume () to run the thread again );
SuspendRequested: The thread is requesting to be suspended but cannot respond;
Unstarted: Thread. Start () is not called to Start the Thread;
WaitSleepJoin: The thread is blocked because it calls methods such as Wait (), Sleep (), and Join;
As mentioned above, the Background State indicates that the thread is running in the Background. What are the special features of the backend running threads? In fact, there is only one difference between the background thread and the foreground thread, that is, the background thread does not prevent program termination. Once all foreground threads of a process are terminated, CLR (general language runtime environment) will completely terminate the process by calling the Abort () method of any surviving background process.
Thread priority
When the threads compete for the CPU time, the CPU is given the service according to the thread priority. In the C # application, you can set five different priorities, from high to low, which are Highest, AboveNormal, Normal, BelowNormal, and Lowest. If the priority is not specified during thread creation, the default value is ThreadPriority. normal.
To specify a priority for a thread, we can use the following code:
// Set the priority to the lowest
MyThread. Priority = ThreadPriority. Lowest;
By setting the thread priority, we can arrange some important threads for priority execution, such as user response.