c#5.0 Asynchronous Programming

Source: Internet
Author: User
Tags semaphore

The BeginInvoke implementation of the delegate is asynchronous

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Threading;namespaceconsoleapplication1{ Public Delegate intMyDelegate (intx); classProgram {Static voidMain (string[] args) {MyDelegate del=NewMyDelegate ((a) ={Console.WriteLine ("Sub Thread Begin"); Thread.Sleep ( -); return 1;            }); //delegate's BeginInvoke method implements asynchronous invocationDel. BeginInvoke (1, (a) = = {Console.WriteLine ("Sub Thread Callback"); },NULL); Console.WriteLine ("Main Thread over");        Console.read (); }    }}
Delegate's BeginInvoke

c#5.0 Async function Linear programming for asynchronous programming

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Threading;namespaceconsoleapplication1{classProgram {Static voidMain (string[] args) {Console.WriteLine ("Main Thread Begin");            MyFunc (); Console.WriteLine ("Main Thread over");        Console.read (); }        Static Async voidMyFunc () {Console.WriteLine ("MyFunc Begin"); //The program executes here to open the asynchronous task            awaitTask.run (() ={Console.WriteLine ("time-consuming operation starts"); Thread.Sleep ( -); Console.WriteLine ("time-consuming operation ends");            }); Console.WriteLine ("MyFunc over"); }    }}
c#5.0 Asynchronous Functions

c#5.0 Async Function Wrapping Method

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Threading;namespaceconsoleapplication1{classProgram {Static voidMain (string[] args) {Console.WriteLine ("Main Thread Begin"); NewTestClass ().            Displayresult (); Console.WriteLine ("Main Thread over");        Console.read (); }    }    classTestClass { Public Async voidDisplayresult () {Console.WriteLine ("time-consuming operation starts"); //asynchronously calling a function encapsulated as a task in an asynchronous function            intres =awaitGetresultasync (2);            Console.WriteLine (RES); Console.WriteLine ("time-consuming operation ends"); }        //encapsulating time-consuming operations into a task function         Publictask<int> Getresultasync (intN) {returnTask.run (() ={Thread.Sleep ( the); returnn +1;        }); }    }}
c#5.0 Asynchronous Functions

Encapsulating functions Asynchronous execution

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Threading;namespaceconsoleapplication1{classProgram {Static voidMain (string[] args) {Console.WriteLine ("Main Thread Begin"); Taskasynchelper.runasync (Newfunc<int,int> (MyFunc),Newaction<int> (Displayresult),2); Console.WriteLine ("Main Thread over");        Console.read (); }        Static intMyFunc (inti) {Console.WriteLine ("time-consuming operation starts"); Thread.Sleep ( the); returni +Ten; }        Static voidDisplayresult (inti) {Console.WriteLine ("The result is:"+i); Console.WriteLine ("time-consuming operation ends"); }    }    //encapsulates a common function that executes asynchronously after passing in a function and after a callback function     Public Static classTaskasynchelper { Public Static Async voidRunasync<tresult> (func<int, tresult> function, Action<tresult> callback,intnum) {            //encapsulates an incoming function as a taskFunc<task<tresult>> Taskfunc = () =            {                returnTask.run (() =                {                    returnfunction (num);            });            }; //asynchronously executes a encapsulated taskTResult res =awaitTaskfunc (); if(Callback! =NULL) {callback (res); }        }    }}
encapsulates an incoming function as an asynchronous execution

Semaphore control Number of threads

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Threading;namespaceconsoleapplication1{classProgram {//declares the semaphore, indicating that the maximum number of threads in the same resource is 3        StaticSemaphoreslim Semlim =NewSemaphoreslim (3); Static voidMain (string[] args) {Console.WriteLine ("Main Thread Begin");  for(inti =0; I < -; i++)            {                NewThread (semaphoretest).            Start (); } Console.WriteLine ("Main Thread over");        Console.read (); }        Static voidsemaphoretest () {semlim.wait ();//blocks the current thread until the semaphore is releasedConsole.WriteLine ("Threads"+thread.currentthread.managedthreadid.tostring () +"execution Start ..."); Thread.Sleep ( -); Console.WriteLine ("Threads"+ Thread.CurrentThread.ManagedThreadId.ToString () +"end of execution ..."); Semlim.release ();//release Semaphore        }    }}
semaphore control Number of threads

c#5.0 Asynchronous Programming

Related Article

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.