C # Multithreading
The thread is defined as the execution path of the program. Each thread defines a unique control flow.
If your application involves complex and time-consuming operations, it is often useful to set up different thread execution paths, each of which performs specific work.
Threads are lightweight processes. A common instance of using threads is the implementation of parallel programming in modern operating systems.
Using threads saves CPU cycles and increases the efficiency of the application.
The program we write so far is a single single process that runs as an instance of the application running. However, the application can only execute one at a time. To perform multiple tasks at the same time, it can be divided into smaller threads.
Thread life cycle
The thread life cycle begins when an object of the System.Threading.Thread class is created, ending when the thread is terminated or when execution is complete.
The following lists the various states in the thread life cycle:
not started state : The condition when a thread instance is created but the Start method is not called.
Ready State : The condition when the thread is ready to run and wait for the CPU cycle.
non-operational status : The following scenarios are not operational:
The Sleep method has been called
The Wait method has been called
Blocking through I/O operations
Death State : The condition when the thread has completed execution or has been aborted.
Main thread
In C #, theSystem.Threading.Thread class is used for threading work. It allows you to create and access a single thread in a multithreaded application. The first thread to be executed in the process is called the main path .
The main thread is created automatically when the C # program begins execution. A thread created using the thread class is called by a child thread of the main thread. You can use the CurrentThread property of the thread class to access the thread.
The following program shows the execution of the main thread:
Using system;using System.threading;namespace multithreadingapplication{class Mainthreadprogram {static Voi D Main (string[] args) {Thread th = thread.currentthread; Th. Name = "Mainthread"; Console.WriteLine ("This is {0}", Th.) Name); Console.readkey (); } }}
When the above code is compiled and executed, it produces the following results:
This is Mainthread
Common properties and methods of the Thread class
The following table lists some of the common properties of the Thread class:
650) this.width=650; "title=" Capture.png "style=" Float:none "src=" http://s3.51cto.com/wyfs02/M00/6D/A6/ Wkiom1vokxpgbn6uaal9iy-akba710.jpg "alt=" Wkiom1vokxpgbn6uaal9iy-akba710.jpg "/>
The following table lists some common methods of the Thread class:
650) this.width=650; "title=" Capture2.png "style=" Float:none "src=" http://s3.51cto.com/wyfs02/M01/6D/A2/ Wkiol1volkfipjbraatd7jjef60941.jpg "alt=" Wkiol1volkfipjbraatd7jjef60941.jpg "/>
650) this.width=650; "title=" Capture3.png "style=" Float:none "src=" http://s3.51cto.com/wyfs02/M00/6D/A6/ Wkiom1vokxtisyidaatdnpk8ess174.jpg "alt=" Wkiom1vokxtisyidaatdnpk8ess174.jpg "/>
650) this.width=650; "title=" Capture4.png "style=" Float:none "src=" http://s3.51cto.com/wyfs02/M01/6D/A6/ Wkiom1vokxsr9yzjaagjlmotjmu659.jpg "alt=" Wkiom1vokxsr9yzjaagjlmotjmu659.jpg "/>
Creating Threads
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:
using system;using system.threading;namespace multithreadingapplication{ 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 "); &nbsP; 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
Managing Threads
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.
using system;using system.threading;namespace multithreadingapplication{ class ThreadCreationProgram { public static void calltochildthread () { console.writeline ("Child thread starts "); // thread paused 5000 milliseconds int sleepfor = 5000; console.writeline ("Child thread 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
Destroying threads
The Abort () method is used to destroy the thread.
Aborts the thread at run time by throwing ThreadAbortException . This exception cannot be captured, and if there is a finally block, the control is sent to the finally block.
The following procedure illustrates this:
using system;using system.threading;namespace multithreadingapplication{ class ThreadCreationProgram { public static void calltochildthread () { try { console.writeline ("Child thread starts"); // Count to 10 for (int counter = 0; counter <= 10; counter++) { thread.sleep (+); console.writeline (counter); } console.writeline ("Child Thread Completed "); } catch (threadabortexception e) { console.writeline ("Thread abort exception"); } finally { console.writeline ("Couldn ' t catch the Thread exception "); } } 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 (); // Stop the main thread for some time thread.sleep (; ) // now aborts the child threads Console.WriteLine ("In main: aborting the child thread"); childthread.abort (); console.readkey (); } }}
When the above code is compiled and executed, it produces the following results:
In main:creating the child threadchild thread starts012in main:aborting the child threadthread Abort exceptioncouldn ' t C Atch the Thread Exception
Reference:
Http://outofmemory.cn/csharp/tutorial/csharp-multithreading.html
This article comes from the "Ricky's blog" blog, please be sure to keep this source http://57388.blog.51cto.com/47388/1656457
C #-Multithreading