委托异步调用方法

血红的双手。 提交于 2020-01-24 01:59:09
        public delegate string CaptureHandler(string ip);

        public class CaptureService
        {
            public static string CapturePicture(string ip)
            {
                Thread.Sleep(3000);
                Console.WriteLine("work done part one");
                return ip.ToString();
            }
        }


        static void Main(string[] args)
        {
            //Thread t = new Thread(new ParameterizedThreadStart(functionA));
            CaptureHandler handler = new CaptureHandler(CaptureService.CapturePicture);

            IAsyncResult result = handler.BeginInvoke("192.168.0.109", new AsyncCallback(SaveDb), handler);
            Console.WriteLine("main work continue");

            Console.ReadKey();
        }

        private static void SaveDb(IAsyncResult ar)
        {
            if (ar.IsCompleted)
            {
                Console.WriteLine("work done part two");
                var handler = ar.AsyncState as CaptureHandler;
                var result = handler.EndInvoke(ar);
                Console.WriteLine(result);

            }
            Console.WriteLine("ok");
        }

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!