Single threaded: Only one thread (main thread), the disadvantage, a time can only call a function method, if you are printing time to close the program, it will not close, there is a phenomenon of suspended animation
Thread is divided into foreground thread and background thread
The default is to create a foreground thread
Foreground thread: Only a few foreground threads are closed to complete the program's shutdown
Background thread: As long as all foreground threads end, the background thread ends automatically
Resolution: The foreground thread that was created can continue to execute after the master thread is closed, and the background threads close as the foreground closes
1. Multi-threading, creating a thread
1 Static voidMain (string[] args)2 {3 //Create a thread to execute the test () method4Thread th =NewThread (Test);5 //start (); Mark the thread is ready to be executed at any time. 7Th. IsBackground =true;//set as Background thread8 //control.invokerequired9 th. Start ();Ten One Console.readkey (); A } - - the Public Static voidTest () - { - -}
The set thread is the foreground thread, the background thread, and the default is the foreground thread
Set to True is a background thread, false is the foreground thread
Th. IsBackground = true; Set as Background thread
Terminates the thread,
Th. Abort ();
If the program is terminated and cannot be opened again, an exception will be thrown and the thread may have been opened or terminated.
Th. Abort ();
Th. Start ();
Cross-thread access is not allowed under. NET if a new thread is to access the main thread's resources
Workaround: Cancel cross-thread access
Sleeps 1000 milliseconds, sleeps on the current thread
Thread.Sleep (1000);
C # Basics _ Multithreading