. NET network programming 1. Multithreading

Source: Internet
Author: User

A preface

1. Why is the network programming to use multithreading?

In network programming often encounter network congestion or network wait, and so on, processing these situations will wait for a long time, such as non-multithreading can occur too long, prone to errors, unresponsive and so on, using multi-threading will greatly improve the program's robustness and scalability of new performance.

2. The difference between a process and a thread

A program at least one process, the image point, you open the QQ is a process, but you also have to chat with several people, open several chat windows, each chat window is a thread, (so the same process can be divided into a number of threads, each thread has its own registers and so on, But the code area is shared)

Two multi-threaded base

Before learning the basis of multi-threaded mail, first give everyone a basic concept of the macro, in this part is divided into 8 parts:

1. Start the thread 2. Terminating a thread 3. Merging Threads 4. Thread Hibernation 5. Thread Priority 6. Synchronization 7. Thread Pool Application

Threads are used in the thread class. His main methods are:

1.Abort terminating thread

2.Interrupt Break Thread

3.join blocks the calling thread until a thread terminates

4.Sleep Sleep

A simple example of a first-come-on:

Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading;using        System.threading.tasks;namespace consoleapplication3{class Program {static void Main (string[] args)                    {Thread thread = new Thread (new ThreadStart () = {while (true) {                    Thread.Sleep (100);                Console.WriteLine ("111");            }            })); Thread.    Start (); Start thread//thread.  Interrupt (); The interrupt thread throws an exception//thread.      Abort ();  Terminating thread Thread.Sleep (2000); The main line blocks 2s thread.   Suspend (); Threads thread2 = new Thread (new ThreadStart () = {//thread.   Start (); There is an exception [the thread is running or is terminated; it cannot be restarted.  ]//Why is there an error? If you want to start restarting an existing process, you can only suspend the previous one and then continue executing//or re-new a thread object thread.    Resume ();         Wakes a suspended thread while (true)       {thread.sleep (100);                Console.WriteLine ("222");            }            })); Thread2.    Start ();        Start thread 2 console.readline (); }    }}

  

. NET network programming 1. Multithreading

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.