Introduction
In the past, ordinary computers had only computing units, but could not perform multiple tasks at the same time. However, the operating system can already run multiple applications at the same time, which implements the concept of multitasking. In order to prevent the possibility that one application controls the CPU and causes other applications and the operating system itself to be suspended forever, the operating system has to use some way to split the physical computing unit into some virtual processes and give each program a certain amount of computing power. In addition, the operating system must always be able to access the CPU first and be able to adjust the priority of accessing the CPU from different programs. Threading is the realization of this concept. A thread can be considered as a virtual process that is used to run a specific program independently. (Remember that threads consume a lot of operating system resources. Sharing a single physical processor for multiple threads will cause the operating system to be busy managing these threads and fail to run the program.)
Next create a console application
class Program { static void Main(string[] args) { Thread t = new Thread(PrintNumbers); (); PrintNumbers(); (); } static void PrintNumbers() { ("Starting..."); for (int i = 1; i < 10; i++) { (i); } } }
As a result, two sets of numbers ranging from 1 to 10 will be randomly crossed. This shows that the printnumbers method runs in both the main thread and another thread.
static void DoNothing() { ((2)); } static void PrintNumbersWithStatus() { ("Starting..."); (()); for (int i = 1; i < 10; i++) { ((2)); (i); } }
Add the following code snippet to the main method
("Starting program..."); Thread t = new Thread(PrintNumbersWithStatus); Thread t2 = new Thread(DoNothing); (()); (); (); for (int i = 1; i < 30; i++) { (()); } ((6)); (); ("A thread has been aborted"); (()); (());
How it works Two different threads are defined when the main program starts. One will be terminated and the other will be successfully completed. The thread state is located in the ThreadState property of the Thread object. The ThreadState property is a C# enumeration object. The thread state is at the beginning. We then start the thread and estimate that in the interval of 30 iterations in a cycle, the thread state will change from. If the actual situation does not match the above, please increase the number of iterations. After terminating the first thread, you will see that the thread's current state is. The program may also print out the status. This fully illustrates the complexity of synchronizing two threads. Remember not to use thread termination in your program. I'm using him here just to show the corresponding thread state. Finally, you can see that the second thread t2 has successfully completed and the status is. There are also some other thread states, but they are either deprecated or are not as useful as the several states we have tried.
This is the end of this article about example code explanation of c# threads (Part 1). For more related c# thread content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!