C# AsyncCallback异步回调用法示例

C# AsyncCallback异步回调用法示例

MSDN上的定义

引用在相应异步操作完成时调用的方法。
命名空间: System
程序集: mscorlib(位于 mscorlib.dll)

跳转至“AsyncCallback 委托”

示例代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        public delegate void MyFunction(string msg);

        public static void Main(string[] args)
        {
            MyFunction fn = StartA;
            fn.BeginInvoke("【B】要开始运行了!", asyncCallback=> {
                for (int i = 1; i <= 1000; i++)
                {
                    Console.WriteLine("\t\t\t【B】运行了" + i + "‰");
                };
            },null);

            Console.WriteLine("【A】要开始运行了!");
            for (int i = 1; i <= 1000; i++)
            {
                Console.WriteLine("\t【A】运行了" + i + "‰");
            };

            Console.ReadKey();
        }


        public static void StartA(string msg)
        {
            Console.WriteLine(msg);
        }
    }
}

运行结果

这里写图片描述

这里写图片描述

参考资料

https://msdn.microsoft.com/zh-cn/library/system.asynccallback.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1
https://www.cnblogs.com/licin/p/8274405.html

猜你喜欢

转载自blog.csdn.net/lgj123xj/article/details/79809719