异步委托

作者:追风剑情 发布于:2016-6-29 11:05 分类:C#

委托异步执行

示例一

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

namespace DelegateTest
{
    public delegate string MyDelegate(object data);

    class Program
    {
        static void Main(string[] args)
        {
            MyDelegate mydelegate = new MyDelegate(TestMethod);
            IAsyncResult result = mydelegate.BeginInvoke("Thread Param", TestCallback, "Callback Param");

            //异步执行完成
            string resultstr = mydelegate.EndInvoke(result);
            Console.WriteLine("resultstr=" + resultstr);

            Console.Read();
        }

        //线程函数
        public static string TestMethod(object data)
        {
            string datastr = data as string;
            return datastr;
        }

        //异步回调函数
        public static void TestCallback(IAsyncResult data)
        {
            Console.WriteLine(data.AsyncState);
        }
    }
}

 

运行测试

111111.png

 

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号