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