C # asynchronous variety of ways of creating a thread calls the basics

A simple way to create a thread is commissioned asynchronous call, the Delegate class provides BeginInvoke method, the method may pass parameters delegate type definition (so that the number of parameters is variable BeginInvoke), in addition to two fixed parameters callback delegate AsynsCallBack and type Object (if you do not use direct assignment to null).

BeginInvoke () return value IAsynResult, through its properties IsComplete based asynchronous call is completed.

        static void Main(string[] args)
        {
            Func<int, int> act1 = new Func<int, int>(Calculate);
            Console.WriteLine("Main Start....");
            IAsyncResult ar = act1.BeginInvoke(20,null, null);
            while (!ar.IsCompleted)
            {
                Console.WriteLine("Main is waiting");
                Thread.Sleep(500);
            }
            int result = act1.EndInvoke(ar);
            Console.WriteLine("Result is " + result);
            Console.ReadLine();
        }

        private static int Calculate(int total)
        {
            int sum = 0;
            for (int i = 0; i < total;i++ )
            {
                sum += i;
                Thread.Sleep(100);
            }
            return sum;
        }

Run returns the result:

Main Start....
Main is waiting
Main is waiting
Main is waiting
Main is waiting
Main is waiting
Result is 190

In fact, EndInvoke also will wait until the end of the asynchronous call, the returned results. In addition, AsyncWaitHandle property IAsynResult is WaitHandle type, use WaitOne () can achieve the same effect as described above, I think there is a benefit that can be used WaitAll () wait for multiple asynchronous call is completed at the same time:

        static void Main(string[] args)
        {
            Func<int, int> act1 = new Func<int, int>(Calculate);
            Func<int, int> act2 = new Func<int, int>(Calculate);
            Console.WriteLine("Main Start....");
            IAsyncResult ar1 = act1.BeginInvoke(20,null, null);
            IAsyncResult ar2 = act2.BeginInvoke(30, null, null);
            if(WaitHandle.WaitAll(new WaitHandle[]{ar1.AsyncWaitHandle,ar2.AsyncWaitHandle},5000))
            {
                Console.WriteLine("Waiting is over");
                
            }
            int result1 = act1.EndInvoke(ar1);
            int result2 = act2.EndInvoke(ar2);
            Console.WriteLine("Result1 is {0},Result2 is {1}", result1, result2);
            Console.ReadLine();
        }

        private static int Calculate(int total)
        {
            int sum = 0;
            for (int i = 0; i < total;i++ )
            {
                sum += i;
                Thread.Sleep(100);
            }
            return sum;
        }

I declare act1 and act2 two delegate objects, and at the same time make asynchronous calls, WaitHandle.WaitAll ( new new WaitHandle [] {ar1.AsyncWaitHandle, ar2.AsyncWaitHandle}, 5000 ) while waiting for the completion of the final return the result:

Main Start....
Waiting is over
Result1 is 190,Result2 is 435

 In addition two or more ways to return to the operation result, can also be used AsynsCallBack callback, BeginInvoke () is the last parameter can be accessed by ar.Asynstate, for use in the callback function, such as transmission delegate instance Get result:

        static void Main(string[] args)
        {
            Func<int, int> act1 = new Func<int, int>(Calculate);
            Console.WriteLine("Main Start....");
            IAsyncResult ar1 = act1.BeginInvoke(20, new AsyncCallback(CalculateComplete), act1);
            while(!ar1.IsCompleted){
                Console.WriteLine("Main is waiting");
                Thread.Sleep(500);
            }
            Console.ReadLine();
        }

        private static int Calculate(int total)
        {
            int sum = 0;
            for (int i = 0; i < total;i++ )
            {
                sum += i;
                Thread.Sleep(100);
            }
            return sum;
        }

        private static void CalculateComplete(IAsyncResult ar)
        {
            Console.WriteLine("Counting is over...........");
            Func<int, int> act = ar.AsyncState as Func<int, int>;
            Console.WriteLine("Result is "+act.EndInvoke(ar));
        }

operation result:

Main Start....
Main is waiting
Main is waiting
Main is waiting
Main is waiting
Main is waiting
Counting is over...........
Result is 190

The callback function is done in the delegate thread, you can also use the Lamda expression, easier, passing the last parameter, because Lamda expression can access outside variables:

        static void Main(string[] args)
        {
            Func<int, int> act1 = new Func<int, int>(Calculate);
            Console.WriteLine("Main Start....");
            IAsyncResult ar1 = act1.BeginInvoke(20, ar => { Console.WriteLine("Result is " + act1.EndInvoke(ar)); }, null);
            while(!ar1.IsCompleted){
                Console.WriteLine("Main is waiting");
                Thread.Sleep(500);
            }
            Console.ReadLine();
        }

 

Guess you like

Origin www.cnblogs.com/change-myself/p/11108980.html
Recommended