SoFunction
Updated on 2025-03-01

C# synchronous and asynchronous call method instances


namespace ConsoleTest
{
    class Program
    {
        static void Main(string[] args)
        {
("*********Synchronous call start****************");
            int result = Add(1,2);
("The synchronous call is completed, the execution result is:" + result);

("*********Asynchronous call start*************");
            SynAdd(1, 2, (r) => {
("Asynchronous call is completed, the execution result is:" + r);
            });
("-------------------------------");
            ();
        }

        /// <summary>
/// Synchronization method
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        static int Add(int a, int b)
        {
            (5000);
            return a + b;
        }

        /// <summary>
/// Asynchronous call
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
/// <param name="callback">Delegate object</param>
        static void SynAdd(int a, int b, Action<int> callback)
        {
            Func<int> func = () =>
            {
                (5000);
                return a+b;
};//Declare the implementation method of asynchronous method
            ((ar) =>
            {
var result = (ar);//The result of the execution after the call is completed
(result);//Trust execution, return the result value
            }, null);
        }
    }
}