Use. NET implementation of multithreading experience Summary _ practical skills

Source: Internet
Author: User

1. Briefly

Generally a program of a process, the code is in the process, the process itself does not execute code, executing code is the thread.

In general a process is a thread. (A shop is a boss.) )

The process is to open up a space in memory. Code, Picture ... And so there is in this space. Code thread to execute.

There is only one thread by default.

Copy Code code as follows:

The systerm.threading//thread operation class is under this namespace.

2. Foreground thread and background thread .

To open a thread is to create a thread object.

Threads are foreground threads by default.

The program will not exit until all foreground threads have finished executing.

The default threads in the process are called the main thread, or the default thread, or the UI thread.

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

Copy Code code as follows:

Thread th = new Thread (Sum);
Th.    Name = "Thread 1"; Give the thread a name. When debugging, you can see the specific name in the output.
Th. IsBackground = true;
Th. Start ();
Th.   Abort (); forcibly terminates the thread.
Thread.Sleep (1000); Pause a thread in milliseconds
Thread Cuth = Thread.CurrentThread Gets a reference to the current thread. Thread type.
Textbox.checkforillegalcrossthreadcalls = false; A cross-thread access check that turns off space. Otherwise, you will receive the following error

3. Thread re-entry

Multiple threads in the same process can be executed "concurrently".

Multiple threads accessing the same resource can cause a different step, which is called a thread reentrant.

This situation should be handled with locking.

Copy Code code 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 Parameter-free method

A delegate object needs to be passed.

Copy Code code as follows:
ThreadStart ts = new ThreadStart (countnum); Thread th = new thread (TS); Equivalent to thread th = new Thread (countnum); Essentially, a delegate object is transmitted. (Countnum is an argument-free method)
4.2 Parametric method

If you need to pass a parameter method.

is the delegate object of the created Parameterizedthreadstart.

The argument is passed in the Start method, and the Start method has two overloads.

Do small partners have any idea about how to use multithreading in. NET, and if you have questions, leave a message.

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.