What are the common operations of Thread threads
This article shows you what the common operations of Thread threads are, which are concise and easy to understand, which can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Create thread
Threads are created by extending the Thread class. The extended Thread class calls the Start () method to start the execution of the child thread.
The following program demonstrates this concept:
Class ThreadCreationProgram {public static void CallToChildThread () {Console.WriteLine ("Child thread starts");} static void Main (string [] args) {ThreadStart childref = new ThreadStart (CallToChildThread); Console.WriteLine ("In Main: Creating the Child thread"); Thread childThread = new Thread (childref); childThread.Start () Console.ReadKey ();}}
When the above code is compiled and executed, it produces the following results:
In Main: Creating the Child thread Child thread starts
Management thread
The Thread class provides a variety of ways to manage threads.
The following example demonstrates the use of the sleep () method to pause a thread at a specific time.
Class ThreadCreationProgram {public static void CallToChildThread () {Console.WriteLine ("Child thread starts"); / / thread pauses for 5000 milliseconds int sleepfor = 5000; Console.WriteLine ("ChildThread Paused for {0} seconds", sleepfor / 1000); Thread.Sleep (sleepfor) Console.WriteLine ("Child thread resumes");} static void Main (string [] args) {ThreadStart childref = new ThreadStart (CallToChildThread); Console.WriteLine ("In Main: Creating the Child thread"); Thread childThread = new Thread (childref); childThread.Start (); Console.ReadKey ();}}
When the above code is compiled and executed, it produces the following results:
In Main: Creating the Child thread Child thread starts Child Thread Paused for 5 seconds Child thread resumes
Destroy thread
The Abort () method is used to destroy the thread.
Abort the thread at run time by throwing a threadabortexception. This exception cannot be caught, and if there is a finally block, the control is sent to the finally block.
The following procedure illustrates this:
Class ThreadCreationProgram {public static void CallToChildThread () {try {Console.WriteLine ("Child thread starts"); / / count to 10 for (int counter = 0; counter)