Multithreading in C #

Source: Internet
Author: User

A) concepts of processes and threads

Popular Will, a process is one of our applications. For example, entering the Calc command on the cmd command line will open the calculator for Windows. In Task Manager, you can see that a process named Calc is running. Such as:

While a process can run multiple threads, the thread is the smallest unit of our program. A thread "parasitic" in the process, it completes a separate task. The process has multiple threads that allow it to perform multiple tasks at the same time, increasing the efficiency of the program.

b) The advantages and disadvantages of multithreading.

Pros: multithreading can perform multiple tasks at the same time, increasing productivity. In particular, it allows multithreading to perform i/0-related operations so that the main thread can continue to execute without blocking.

  Disadvantage: multithreading only in the multi-core cup system in order to show the advantages. Because multiple threads are running in a process, it takes time and resource overhead for the system to switch between different threads. In the case of a single-core cup, using multithreading can result in lower efficiency due to thread switching. At the same time, multithreading involves resource sharing, code is not easy to write, debugging and other issues. So try not to use multithreading when you don't need multiple threads.

III). NET multithreaded Code instances

Using multi-threading, you need to introduce a using System.Threading; namespaces. Instantiate a thread object using the thread class, and then call the start () overloaded method to open the thread.

1) Multi-threaded function without parameters.

        Static voidMain (string[] args) {Thread worker=NewThread (printalllist); Worker. Start (); }         Public Static voidprintalllist () { for(inti =0; I <Ten; i++) {Console.Write (i+" ");        } Console.WriteLine (); }

2) Multi-threaded operation with parametric functions

        Static voidMain (string[] args) {Thread worker=NewThread (printalllist); Worker. Start (4); }         Public Static voidPrintalllist (Objecttarget) {             for(inti =0; I < (int) Target; i++) {Console.Write (i+" ");        } Console.WriteLine (); }

By instantiating a thread instance, you can generate one, and then pass in the thread's constructor to pass in a function name to execute. This actually allows the system to open a new thread, allowing the newly-opened thread to execute the code corresponding to the function name, so that the main thread is not blocked to continue execution.

It is important to note that when a function is passed into a multithreaded constructor, the parameter type of the function must be object, otherwise there will be a compilation error. In that case, how can we ensure that the function code that our newly opened thread runs can use certain types of parameters? That is, if I were to the worker. Start (4) is written as a worker. Start ("Hello"), our example above will have a run-time error. How can you avoid it? The answer is that we can write the Printalllist function in the wrapper class through a wrapper class. When a parameter is passed in the constructor of the wrapper class, the compile-time type security check is guaranteed. The code is as follows:

    classProgram {Static voidMain (string[] args) {Printwarpper Printwarpper=NewPrintwarpper (5); //printwarpper printwarpper = new Printwarpper ("Hello");Thread worker =NewThread (printwarpper.printalllist); Worker.        Start (); }    }    classPrintwarpper {Private int_target;  PublicPrintwarpper (int_target) {             This. _target =_target; }         Public voidprintalllist () { for(inti =0; I < This. _target; i++) {Console.Write (i+" ");        } Console.WriteLine (); }    }

The above code implements the type safety check at compile time. If we pass a string (such as a Comment line code) into the Printwapper constructor, we can find the error at compile time.

Multithreading in C #

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.