A single threaded introduction to the C # Threading Starter Tutorial

Source: Internet
Author: User


What is a process?

When a program is opened and run, it is a process. Include threads in the process, which can consist of one or more threads.


What is a thread?

A thread is the smallest unit of a program's execution flow. A standard thread consists of a thread ID, a current instruction pointer (PC), a collection of registers, and a stack.


What is multithreading?

Multithreading refers to a program that contains multiple execution streams, in which multiple different threads can be run concurrently to perform different tasks, that is, allowing a single program to create multiple threads of parallel execution to accomplish their tasks.


Personal Summary

In C # We open an application that opens a process that includes a main thread. We can do this on the basis of adding a single or multiple threads of our own writing to perform the tasks we want to accomplish.

The same method that we commonly use in thread threading classes:

Start (): Start thread;

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

Abort (): This method is usually used to terminate a thread;

Suspend (): The method does not terminate the unfinished thread, it simply suspends the thread and can be resumed later;

Resume (): Resumes execution of a thread that was suspended by the suspend () method.

Open a thread in C # to execute the method we wrote. Mainly through the ThreadStart proxy to achieve. Here are some simple code examples.


code example

Single Thread:


The code is as follows:


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; Setting the current child thread as a background thread means that when the main thread is closed, the other child threads are closed at the same time

Threada.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 <; i++)

{

Console.WriteLine ("A");

}

}

<summary>

Threaded with parameters

</summary>

<param name= "Parameter" > can only define an object parameter because the delegate Parameterizedthreadstart is a single-argument object type </param>

static void Threadmethodparameter (object Parameter)

{

for (int j = 0; J <; J + +)

{

Console.WriteLine ("B" +parameter);

}

}

}

}

Open two threads respectively in the agent.

The first thread Threada without parameters, and the thread runs to execute the Threadmethod () method.

The second thread threadb with an object parameter and executes the Threadmethodparameter (object Parameter) method with the Parameterizedthreadstart delegate. This thread is calling start () When you pass in the required parameters.

(When writing the code, I wonder why the thread with parameters can only pass an object parameter?) See the code to know Parameterizedthreadstart only one parameter is not overloaded).

Execution Result:

Main write:m is printed for the main thread.

A: Print for thread threada.

BP: threadb printing for thread with parameters.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
A single threaded introduction to the C # Threading Starter Tutorial

This address: http://www.paobuke.com/develop/c-develop/pbk23128.html






Related content C # development Portal and Applications (1) interface using C # in WinForm to implement data additions and deletions and other functions C # hide mobile phone number, mailbox and other sensitive information implementation Method C # implements the method of converting the commodity amount lowercase to uppercase
Regular expressions in C # introduces. NET random generation of Chinese character C # insertion Algorithm example Analysis C # Implementation of compression, cropping operation instance of picture file

A single threaded introduction to the C # Threading Starter Tutorial

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.