C # multithreading technology (I)

Source: Internet
Author: User

The company's projects may be written in C #. If it is used, it will inevitably involve multithreading technology. The basic concepts are the same, and the differences are specific.Code.

 

In C #, the thread function execution is encapsulated in the class and starts the thread to execute the function through the start function of the Class Object. The thread class thread accepts a threadstart delegate object. The specific code should be as follows:

 

1 Using System;
2 Using System. Collections. Generic;
3 Using System. LINQ;
4 Using System. text;
5 Using System. Threading;
6
7 Namespace Multithreadtest
8 {
9 Class Program
10 {
11 Static   Void Main ( String [] ARGs)
12 {
13 Thread thread1 =   New Thread (
14 Delegate ()
15 {
16 Console. writeline ( " Hello here! " );
17 }
18 );
19 Thread thread2 =   New Thread (
20 () => // Lambda expressions
21 {
22 Console. writeline ( " Well then goodbye! \ N " );
23 }
24 );
25
26 Thread1.start ();
27 Thread2.start ();
28 }
29 }
30 }
31

 

In multithreading, the system will wait until all threads are finished before it exits. At this time, you can call the join function of the thread object. When the function's online process ends at the end of the end, the thread will return the result.

 

When there are many threads, the join function without parameters needs to be called multiple times, which is very time-consuming and laborious. In this case, you can call the join function with time parameters. When the time exceeds the limit, the system forces the thread to stop running, as shown in the following code:

 

1 If ( ! Thread. Join ( 30000 ))
2 {
3 Thread. Abort ();
4 }

 

 

Previously processed thread functions cannot save and process the data status. This is obviously not suitable for complex applications. Thread in C # uses threadstart to delegate data, this problem can be effectively solved:

 

Code 1 Class Threadedtask {
2 String _ Whattosay;
3 Public Threadedtask ( String Whattosay ){
4 _ Whattosay = Whattosay;
5 }
6
7 Public   Void Methodtorun (){
8 Console. writeline ( " I am babbling ( "   + _ Whattosay +   " ) " );
9 }
10 }
11
12
13 Threadedtask =   New Threadedtask ( " Hello " );
14
15 Thread thread =   New Thread (
16 New Threadstart (task. methodtorun)
17 );
18 Thread. Start ();

 

The Hello string is present in the class. The methodtorun method is delegated to the thread and the start function is called. At this time, the data stored in the class can be accurately obtained.

 

 

 

 

 

 

 

 

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.