C # Multithreading

Source: Internet
Author: User

First, the basic concept

1, Process: When a program starts running, it is a process. Processes include resources that are used by programs and programs that are running. And a process is made up of multiple threads.

2, Thread: A thread is an execution flow in a program, each thread has its own proprietary register (stack pointers, program registers, etc.). However, the code area of the program is shared, that is, different threads can perform the same function.

3, Multithreading: Refers to the program contains a number of execution flow, that is, in one program can run multiple different threads to perform different tasks. In other words, it allows a single program to create multiple threads of parallel execution to accomplish their tasks.

4, any program at the time of execution, at least one main thread.

5. All classes related to multithreaded applications are placed in the System.Threading namespace.

6. The following important methods are included in the thread class:

Start (): Start thread;

Sleep (int): Static method that pauses the specified number of milliseconds for the current thread;

Abort (): terminates a thread;

Suspend (): Hangs thread, can restore later;

Resume (): Resumes a suspended thread;

Ii. steps to create a simple thread

We use a simple console application as a demonstration, as shown in the code:

The entry of the thread is provided through the ThreadStart Proxy (delegate), and we can interpret ThreadStart as a function pointer pointing to the function that the thread is going to execute. When the program calls the Thread.Start () method, the thread begins executing the function that ThreadStart represents or points to.

Third, the priority level of the thread

When competing for CPU resources between different threads, the CPU is given the service according to the priority of the thread. In a C # application, a user can set 5 different priority levels for a thread, from high to Low, respectively: Highest,abovenormal,normal,belownormal,lowest.

If you do not specify a priority when creating a thread, the decency defaults to Threadpriority.normal.

To assign a priority to a thread, you can use the following code:

Newthreading. Priority=threadpriority.lowest;

Four, multi-threaded with parameters

1. Case with one parameter

Initializes a new instance of the thread class with thread (Parameterizedthreadstart), specifying a delegate that allows an object to be passed to the thread when it is started.

Note: Parameterizedthreadstart requires that the parameter type must be object, so the formal parameter type of the method defined in the program must be object.

2, with multiple parameters of the case

Because thread provides only the ThreadStart and Parameterizedthreadstart two constructors by default, if you need to pass multiple arguments, we can use the arguments ourselves as properties of the class. This property is instantiated when the class object is defined and then manipulated.

In the above program for simplicity we only set a property of text. In the actual operation, if you want to pass in more than one parameter, set multiple properties.

C # Multithreading

Related Article

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.