C # Processes and threads

Source: Internet
Author: User

I. Processes and Threads

Process is the dynamic execution process of a static instruction sequence, and it is the basic unit of system resource allocation and dispatch. The process-related information includes the user flags of the process, the compiled program that is being executed, the location of the program and data in memory, and so on. The same process can be divided into a number of independent execution flows, called threads. Threads are the basic unit of CPU scheduling. For example, in a Windows environment, users can run multiple applications at the same time, and each application that is thanked is a process.

The partition of process and thread is very important to improve the parallelism of software, and realize time-sharing processing.

Second, Process class

The process class is a related processing task that is used to complete related processes, which can start and stop processes on a computer, or query information about a process. The general way to start a process is to:

(1) First create an instance of the process class;

(2) Set the StartInfo property of the object;

(3) Call the object's Start method to start the process;

Third, the thread class

The thread class is used to create and control threads, which have the following actions: Starting a thread, terminating the thread, merging the thread, and letting the thread hibernate.

1. Start thread

First create a thread

Thread t=new thread (enterpoint);//enterpoint is the thread's entry, the method that the thread executes.

2. Terminating threads

There are two ways to do this: set a Boolean variable beforehand, in which other threads can modify the value two of the variable passed to the thread, as a judgment condition for whether it needs to terminate, and loop through the variable in that thread to determine whether to exit the thread. This method is generally used in practical programming. Method Two: Forcibly terminates the thread by calling the thread class's Abort method.

3. Merging threads

The Join method is used to merge two threads that are executing concurrently into a single thread. If a thread T1 in the process of execution and waits for another thread to finish T2 to continue, the T2 join method can be called in the T1 program module.

4. Let the thread hibernate

Call the sleep method of the thread class, which is to let the thread hibernate. such as Thread.Sleep (1000);

5. Thread Priority

When a thread competes for CPU time, the CPU is serviced according to the priority of the thread. If set thread T priority is highest, t.priority=threadpriority.highest;

6. Thread pool

7. Synchronization

Synchronization refers to the correlation between multiple threads that have sequential execution. You can use the lock statement to Cheng multiple lines of logic.

C # Processes and 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.