process; Thread Threads

Source: Internet
Author: User
Tags thread stop

<>


foreground thread and background threadinvokehelper: Cross-thread access/Modify main interface control methods, properties C # thread usage and cross-thread access
Using system;using system.collections.generic;using system.linq;using system.text;using System.Diagnostics; namespace process process{///<summary>///Process class is a non-static class. It contains both static and non-static members.            Static members have classes called directly, non-static members have classes of objects to invoke//</summary> class Program {static void Main (string[] args) { Each of the user programs is a process. This class of operating processes in. NET is called process process[] Pros = process.getprocesses (); Gets all the processes that are running on the current computer. Store into a process array (process[] pros) foreach (var item in pros) {//item. Kill ();//Stop the current process immediately. That is: Kill the current process. This is not a mess.                Write in this loop, kill all the running process, MD used once to my computer is closed, too dangerous. Console.WriteLine (item);            Output each process to the console.            }//process to open some applications//START process resources by specifying the name of the document or application file: 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 Drawing tool Process.Start ("IExplore", "Http://www.baidu.com");            Open the browser, open the Baidu//process to open some of the files process p = new process (); AndSpecifies the file name, such as the application or document, to use when starting the process.            ProcessStartInfo psi = new ProcessStartInfo (@ "C:\Program Files (x86) \tencent\qq\qqprotect\bin\qqprotect.exe");            StartInfo: It represents the data p.startinfo=psi to be used when starting the process;  P.start (); Start Process}}}


Thread

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading;namespace Thread threads {///<summary>/////////When a program starts, a process is created by the operating system (OS), while a thread runs immediately, which is often called the main thread of the program (main//thread) because it is a program    Executed at the beginning, if you need to create the thread again, the thread created is the child thread of the main thread. A process is made up of multiple threads. There are two types of threads, one called the foreground thread.    The other is called a background thread.    Foreground thread: The application must run out of all foreground threads to exit; (the main thread is the foreground thread)///Background thread: While for a background thread, the application can exit without considering whether it has finished running, and all background threads will automatically end when the application exits. </summary> class Program {public static void Test () {for (int i = 0; i < 100;            i++) {Console.WriteLine (i);        } console.readkey (); }//single-threaded program execution, the running path (the processing of things) is sequential down, must be handled well before, the later will be executed. (This is prone to "suspended animation" and so on.) For example: A foreground thread did not complete, the program can not quit, the card is not moving, causing suspended animation, not to wait until the front-end threads are executed, to quit the program. Another example: a console program that starts with 10 threads and runs for 5 seconds per thread. Because the thread's IsBackground property defaults to False, that is, they are all foreground threads, so even though the main thread of the program runs out quickly, the program ends up running until all the threads that have started are run, and the purpose of multithreading is to let the computer "simultaneously" do multiple things, saving time. Multithreading is a common way to improve the running efficiency and performance of programs.With technology static void Main (string[] args) {Console.WriteLine ("I don't want to Eat Now");//The main thread completes the output. --This is the main thread of the Cheng Gan//Create a thread. Let this thread execute the test () method. (Note that this method does not have parentheses)//By default we create this thread called "Foreground thread".        We can also set him as a "background thread", thread th = new Thread (Test); This is the Cheng Gan of th line. IsBackground = true;//will just create this line of th Cheng as "background thread" th. Start ();//Mark this thread is ready to be executed at any time by the CPU (when the thread is going to have a CPU decision, we cannot manually go to the calling thread, we can do is to tell the CPU that the thread is ready and you can execute it at any time). If the CPU is busy at this time, it still can not execute this thread, if not busy, it is possible to execute the thread immediately Console.WriteLine ("I Want to eat");  The main thread completes the output-this is the main line Cheng Gan for (int i = 101; i <; i++) {Console.WriteLine (i); The main thread completes the output-this is the main thread Cheng Gan}//Under. NET is not allowed to cross-thread access.             The method to resolve cross-thread access is: Control.checkforillegalcrossthreadcalls = false; This Checkforillegalcrossthreadcalls property indicates whether a call to the wrong thread is captured, or false to indicate that the call to the wrong thread is not checked. If true, the call to the wrong thread is checked.            "This control class is the base class for all controls in WinForm" Thread.Sleep (3000);//Let the current thread stop for 3 seconds before running. Console.WriteLine ("Hellow World"); }    }}





process; Thread Threads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.