C # thread -- the basis of the first single thread,

Source: Internet
Author: User

C # thread -- the basis of the first single thread,

Concept

 

What is a process?

When a program is opened and running, it is a process. A process includes threads. A process can be composed of one or more threads.

What is a thread?

A thread is the smallest unit of the program execution flow. A standard thread consists of the thread ID, current Instruction Pointer (PC), register set, and stack.

What is multithreading?

Multithreading means that a program contains multiple execution streams, that is, a program can run multiple different threads to execute different tasks at the same time, that is to say, a single program is allowed to create multiple parallel threads to complete their respective tasks.

 

Personal Summary

In C #, when we open an application, a process is opened, which includes a main thread. On this basis, we can add one or more threads we write to execute the tasks we want to complete.

In the Thread class, we often use the same method:

    • Start (): Start the thread;
    • Sleep (int): a static method that pauses the specified number of milliseconds of the current thread;
    • Abort (): This method is usually used to terminate a thread;
    • Suspend (): This method does not terminate the unfinished thread. It only suspends the thread and can be recovered later;
    • Resume (): Resume the execution of threads suspended by the Suspend () method.

Open a thread in C # To execute the method we wrote. It is mainly implemented through ThreadStart proxy delegation. Below are some simple code examples.

 

Sample Code

Single thread:

Using System; using System. threading; namespace StudyThread {class Program {static void Main (string [] args) {OneThread ();} static void OneThread () {Thread threadA = new Thread (ThreadMethod); threadA. isBackground = true; // set the current sub-thread to a background thread, which means that after the main thread is closed, all other sub-threads will close threadA at the same time. start (); object ThreadParameter = "P"; Thread threadB = new Thread (new ParameterizedThreadStart (ThreadMethodParameter); threadB. isBackground = true; threadB. start (ThreadParameter); Console. writeLine ("Main Write: M"); Console. readLine ();} static void ThreadMethod () {for (int I = 0; I <10; I ++) {Console. writeLine ("");}} /// <summary> /// thread with parameters /// </summary> /// <param name = "Parameter"> only one object Parameter can be defined, because the entrusted ParameterizedThreadStart is of the Single-Parameter object type </param> static void ThreadMethodParameter (object Parameter) {for (int j = 0; j <10; j ++) {Console. writeLine ("B" + Parameter );}}}}

Enable two threads in the proxy respectively.

The first thread, ThreadA, does not have any parameters. When the thread is running, it executes the ThreadMethod () method.

ThreadB of the second thread carries an Object Parameter and runs the ThreadMethodParameter (object Parameter) method through the ParameterizedThreadStart delegate. This thread passes in the required parameters when calling Start.

(When I write code, I wonder why the thread with parameters can only pass one object parameter? After reading the code, we can see that there is only one ParameterizedThreadStart parameter that is not overloaded ).

 

Execution result:

 

Main Write: M is printed on the Main thread.

A: print for the thread ThreadA.

BP: print for ThreadB with parameter.

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.