Summary of multi-thread implementation experience using. Net, and. net multi-thread experience

Source: Internet
Author: User

Summary of multi-thread implementation experience using. Net, and. net multi-thread experience

1. Brief Introduction

Generally, a program is a process in which the code exists. The process itself does not execute the code, and the code is executed by a thread.

Generally, a process is a thread. (A store is a lady in charge .)

A process opens up a space in the memory. Code, image .. And so on. Run the code thread.

By default, there is only one thread.

Copy codeThe Code is as follows:
Systerm. threading // the class for thread operations is in this namespace.

2. foreground and background threads.

To enable a thread, create a thread object.

By default, all threads are foreground threads.

The program will exit after all foreground threads are executed.

The default thread in a process is the main thread, the default thread, or the UI thread.

Background threads. As long as all foreground threads end, all background threads end directly.

Copy codeThe Code is as follows:
Thread th = new Thread (Sum );
Th. Name = "thread 1"; Name the thread. You can see the specific name in the output during debugging.
Th. IsBackground = true;
Th. Start ();
Th. Abort (); forcibly terminate the thread.
Thread. Sleep (1000); pause the Thread in milliseconds
Thread cuTh = Thread. CurrentThread to get the reference of the current Thread. Thread type.
TextBox. checkforillegalcrossthreadcils = false; // disable cross-thread access checks for space. Otherwise, the following error occurs:

 

3. Thread re-import

Multiple threads in the same process can be executed concurrently.

Multiple Threads access the same resource, which may cause non-synchronization. This is called thread re-entry.

In this case, locks can be applied.

Copy codeThe Code is as follows:
Private void CountNum ()
{
Lock (this)
{
For (int I = 0; I <10000; I ++)
{
Int num = int. Parse (textBox1.Text. Trim ());
Num ++;
TextBox1.Text = num. ToString ();
}
}
}

4. thread object nature (delegate) 

4.1 No Parameter Method

You need to upload a delegate object.Copy codeThe Code is as follows: ThreadStart ts = new ThreadStart (CountNum); Thread th = new Thread (ts); equivalent to Thread th = new Thread (CountNum); essentially a delegate object. (CountNum is the method without parameters) 4.2 The method with Parameters

If you need to pass the parameter method.

It is the delegate object of the ParameterizedThreadStart created.

Parameters are passed in the Start method. The start method has two overloading methods.

Are you familiar with multithreading in. net? If you have any questions, please leave a message.

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.