Process; Thread
<>
Foreground thread and background thread InvokeHelper: Cross-thread access/modification of the main interface control method, attribute C # thread usage and cross-thread access
Using System; using System. Collections. Generic; using System. Linq; using System. Text; using System. Diagnostics; namespace Process {////// The Process class is a non-static class. It contains static and non-static members. Static members can be called directly by class, and non-static members can be called by class objects ///Class Program {static void Main (string [] args) {// each user Program is a process. In. net, this class is called Process [] pros = Process. GetProcesses (); // gets all processes running on the current computer. Store it in a Process array (Process [] pros) foreach (var item in pros) {// item. Kill (); // stop the current Process immediately. That is, kill the current process. This cannot be used in disorder. In this loop, all running processes are killed. It is too dangerous for MD to shut down all the processes on my computer once. Console. WriteLine (item); // output each process to the Console.} // Open some applications through processes // start process resources by specifying the document or application file name: the parameter is: the name of the document or application file to run in the process. Process. start ("calc"); // open the calculator Process. start ("mspaint"); // open the plotting tool Process. start ("iexplore", "Http: // www.baidu.com"); // open the browser, open Baidu // open some files through the Process p = new Process (); // specify the file name of the application or document used to start the process. ProcessStartInfo psi = new ProcessStartInfo (@ "C: \ Program Files (x86) \ Tencent \ QQ \ QQProtect \ Bin \ QQProtect.exe"); // StartInfo: it indicates the data p. startInfo = psi; p. start (); // Start the process }}}
Thread
Using System; using System. Collections. Generic; using System. Linq; using System. Text; using System. Threading; namespace Thread {////// When a program starts, a process is created by the operating system (OS), and a thread runs immediately, this Thread is usually called the Main Thread of the Program (Main // Thread), because it is executed at the beginning of the program. If you need to create another Thread, the created thread is the subthread of the main thread. ///// A process is composed of multiple threads. There are two types of threads: Front-End threads. The other is the background thread. /// Foreground thread: The application can exit only after running all foreground threads. (the main thread is the foreground thread.) // background thread: For background threads, the application can exit directly regardless of whether it has been run. All background threads automatically end when the application exits. //////Class Program {public static void Test () {for (int I = 0; I <100; I ++) {Console. writeLine (I);} Console. readKey () ;}// when a single-threaded program is executed, the path of the Program (processed items) runs in a continuous order and must be processed properly, will be executed later. (This is prone to "False-dead" issues. For example, if the execution of a foreground thread is not completed, the program cannot be exited and the program is stuck there, causing a false death. The program can be exited only when the foreground thread is finished. For example, in a console program, the program starts 10 threads at the beginning, and each thread runs for 5 seconds. Because the IsBackground attribute of threads defaults to false, that is, they are all Front-End threads, even though the main thread of the program soon stops running, but the program will not end until all started threads are completed) // The purpose of Multithreading is to allow the computer to "do multiple things at the same time" to save time. Multithreading is a common technique used to improve program running efficiency and performance. static void Main (string [] args) {Console. writeLine ("I don't want to eat now"); // The main thread completes the output. -- This is what the main thread does // create a thread. Let this thread execute the Test () method. (Note that do not use parentheses for the Test method) // by default, the thread we created is called "foreground thread ". We can also set it to "background Thread" Thread th = new Thread (Test); // -- this is what th does. isBackground = true; // set the created th thread to "background thread" th. start (); // indicates that the thread is ready and can be executed by the CPU at any time. (when the thread is executed, the CPU determines the time. We cannot manually call the thread, all we can do is tell the CPU that this thread is ready and you can execute it at any time. If the CPU is busy at this time, it still cannot execute this thread. If it is not busy, it may immediately execute this thread) Console. writeLine ("I want to eat"); // The main thread completes the output -- this is what the main thread does for (int I = 101; I <200; I ++) {Console. writeLine (I); // The main thread completes the output -- this is what the main thread does} // In. net is not allowed to access across threads. The solution to cross-thread access is: Control. checkforillegalcrossthreadcils = false; // The checkforillegalcrossthreadcils attribute indicates whether to capture calls to error threads. If it is false, it indicates not to check calls to error threads. If it is true, it indicates checking the call to the wrong thread. [This Control class is the base class of all controls in WinForm] Thread. Sleep (3000); // stop the current Thread for 3 seconds and then run it again. Console. WriteLine ("Hellow world ");}}}
There are already too many other