C # multi-threaded programming-use a delegate to create a thread

Source: Internet
Author: User

Processes and threads

Process: A process contains resources, such as a Windows handle, a file system handle, or other core objects. Each process is allocated virtual memory. A process contains at least one thread. The Operating System Schedules threads.

Process Management resources include virtual memory and Windows handle.

 

Thread: Each thread has its own stack,ProgramCodeMemory and heap are shared by all threads in a process.

 

In. net, managed threads are defined by the Thread class. Managed threads are not necessarily mapped to an operating system thread.

 

Three ways to create a thread using a delegate

1. Pooling voting method

2. waithandle wait handle

3. asynchronous call asynchronous callback

 // Asynchronous delegation example  // Herbert  // For personal learning and use  Using System; Using System. Collections. Generic; Using System. LINQ;Using System. text; Using System. Threading; Using System. diagnostics; Namespace Asydelegate { Class Program { Public   Delegate   Int Takesawhiledelegate ( Int Data, Int MS ); Static   Int Takesawhile ( Int Data, Int MS) {console. writeline ("Takesawhile started" ); Thread. Sleep (MS); console. writeline ( "Talesawhile completed" ); Return ++ Data ;} Public   Static   Void Method1 (){ // Method 1              // Synchronouse method call              // Takesawhile (1, 3000)              // Asynchronous by using a delegate              // Iasyncresult can obtain the delegate information and verify whether the delegate has completed the task. Takesawhiledelegate DL = takesawhile; iasyncresult AR = DL. begininvoke (1, 3000, Null ,Null ); While (! Ar. iscompleted ){ // Do something else in the main thread Console. Write ( "." ); Thread. Sleep (50 );} Int Result = DL. endinvoke (AR); console. writeline ( "Result: {0 }" , Result );} Public   Static   Void Method2 (){ // Method 2 waiting for handle              // Synchronouse method call             // Takesawhile (1, 3000)              // Asynchronous by using a delegate              // Waitone () uses a time-out period as the first optional parameter and defines the maximum time to wait Takesawhiledelegate DL = takesawhile; iasyncresult AR = DL. begininvoke (1, 3000, Null , Null ); While ( True ){ // Do something else in the main thread Console. Write ( "." ); If (AR. asyncwaithandle. waitone (50, False ) {Console. writeline ( "Can get the result now" ); Break ;}} Int Result = DL. endinvoke (AR); console. writeline ( "Result: {0 }" , Result );} Public   Static   Void Takesawhilecompleted (iasyncresult AR ){ // Method 3: asynchronous callback              If (AR = Null ) Throw   New Argumentnullexception ( "Ar" ); Takesawhiledelegate d1 = ar. asyncstate As Takesawhiledelegate; trace. Assert (d1! = Null , "Invalid object type" ); Int Result = d1.endinvoke (AR); console. writeline ( "Result: {0 }" , Result );} Static   Void Main ( String [] ARGs) {stopwatch SW1 = New Stopwatch (); console. writeline ( "Method 1 pooling started :" ); Sw1.start (); program. Method1 (); sw1.stop (); console. writeline (sw1.elapsedmilliseconds. tostring (); stopwatch sw2 = New Stopwatch (); console. writeline ( "Method 2 wait handler started :" ); Sw2.start (); program. method2 (); sw2.stop (); console. writeline (sw2.elapsedmilliseconds. tostring (); stopwatch sw3 = New Stopwatch (); console. writeline ( "Method 3 asynchronize callback started :" ); Sw3.start (); takesawhiledelegate d1 = takesawhile; /*  D1.begininvoke (1, 3000, AR => {  Int result = d1.endinvoke (AR );  Console. writeline ("Result: {0}", result );  }), Null;  */ D1.begininvoke (1, 3000, takesawhilecompleted, D1 ); For ( Int I = 0; I <100; I ++) {console. Write ( "." ); Thread. Sleep (50);} console. writeline (sw3.elapsedmilliseconds. tostring (); console. readkey ();}}}

 

By comparing the running time, the voting method and asynchronous callback are easier to use. It seems that the voting method is less efficient, while asynchronous callback is the most efficient.

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.