Use of asynchronous delegation

Keywords: PHP

I. Synchronized Delegation

Delegation types can be divided into synchronous delegation and asynchronous delegation. Synchronous delegation is invoked through Invoke. If a more tedious task is to be invoked, synchronous delegation will block and make the program stagnate for a long time, until the work in the delegation is completed, it can continue to execute, which will cause very bad use. Household physical examination.

The following example shows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DelegateInvoke
{
    class Program
    {
        private delegate float DelegaetAdd(float a, float b);
        static void Main(string[] args)
        {
            DelegaetAdd myDelegate = new DelegaetAdd(Add);
            float subNum = myDelegate.Invoke(5, 6);
            Console.WriteLine("The main thread continues to execute...");
            Console.WriteLine("The value is{0}", subNum);
            Console.ReadKey();
        }

        private static float Add(float a, float b)
        {
            Console.WriteLine("Executing...");
            Thread.Sleep(2000);

            return (a + b);
        }
    }
}

Print results:

II. Asynchronous delegation

Because synchronous delegation causes program jamming and bad user checkup, it is better to use asynchronous delegation at this time; asynchronous delegation is implemented by BeginInvoke and Endinvoke calls.

The following example shows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DelegateInvoke
{
    class Program
    {
        private delegate float DelegaetAdd(float a, float b);
        static void Main(string[] args)
        {
            DelegaetAdd myDelegate = new DelegaetAdd(Add);
            IAsyncResult result = myDelegate.BeginInvoke(5, 6, null, null);
            Console.WriteLine("The main thread continues to execute...");
            float subNum = myDelegate.EndInvoke(result);
            Console.WriteLine("The value is{0}", subNum);
            Console.WriteLine("Execution completed...");
            Console.ReadKey();
        }

        private static float Add(float a, float b)
        {
            Console.WriteLine("Executing...");
            Thread.Sleep(2000);

            return (a + b);
        }
    }
}

Print results:


From the printed results, we can see that the main thread does not wait, but runs directly, but we can find that when the main thread runs to EndInvoke, if the call has not ended at this time, then in order to wait for the result of the call, the thread will still block waiting for the call to complete.

3. Asynchronous callback

It can be seen that neither of the above two methods can solve the problem of blocking when calling, then it is time for the protagonist to come on the stage, that is, asynchronous callback. With callback function, when the call ends, the callback function will be automatically called, which can solve the problem of blocking the main thread in order to wait for the result of the call.

The following example shows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DelegateInvoke
{
    class Program
    {
        private delegate float DelegaetAdd(float a, float b);
        static void Main(string[] args)
        {
            DelegaetAdd myDelegate = new DelegaetAdd(Add);
            AsyncCallback callBack = new AsyncCallback(CallBack);
            IAsyncResult result = myDelegate.BeginInvoke(5, 6, callBack, "Completed");
            Console.WriteLine("The main thread continues to execute...");
            Console.ReadKey();
        }

        //Asynchronous callback function
        private static void CallBack(IAsyncResult result)
        {
            DelegaetAdd myDelegate = (DelegaetAdd)((AsyncResult)result).AsyncDelegate;
            if(result.IsCompleted)
            {
                Console.WriteLine("Executing...");
                Thread.Sleep(2000);
                Console.WriteLine("The value is{0}", myDelegate.EndInvoke(result));
                Console.WriteLine(result.AsyncState);
            }     
        }

        private static float Add(float a, float b)
        {
            return (a + b);
        }
    }
}

Print results:

The BeginInvoke() method is used to start the execution of an asynchronous delegation, and the EndInvoke() method is used to end the asynchronous delegation and obtain the return value after the completion of the asynchronous delegation. IAsyncResult.IsCompleted is used to monitor the execution status (true / false) of an asynchronous delegate, and this property will not return true until the asynchronous delegate is executed. BeginInvoke() can accept many parameters, its parameters and parameters type depend on the number and type of parameters when defining delegation, but no matter how many parameters it has, the last two parameters are invariant; the first is the delegation type AsyncCallBack(IAsyncResult result), and the second is user-defined. The data type, IAsyncResult.AsyncState, is its value.

Posted by backinblack on Sun, 21 Jul 2019 20:47:20 -0700