task 异步

class Program
    {
        delegate void del_pfunc(string par);

        static void Main(string[] args)
        {
            Console.WriteLine("main process");
            //同步
            //pfunc("process #2");

            //老方法,异步委托
            //del_pfunc dp = new del_pfunc(pfunc);
            //dp.BeginInvoke("process #2", null, null);

            //新方法
            Task task = new Task(() => Console.WriteLine("process #2"));
            task.Start();
        }

        static void pfunc(string par)
        {
            Thread.Sleep(2000);
            Console.WriteLine(par);
        }
    }

 

    public class asyncTestcs
    {
        delegate void del_pfunc(string par);

        public static void doTest()
        {
            del_pfunc dp = new del_pfunc(pfunc); //委托
            //方法参数……,回调方法,参数
            dp.BeginInvoke("process #2", new AsyncCallback(endrecv), "par999");
        }

        static void pfunc(string par)
        {
            Thread.Sleep(2000);
            Console.WriteLine(par);
        }

        //完成后回调
        static void endrecv(IAsyncResult ar)
        {
            //x.EndGetResponse(ar); endxxx方法的传入参数为ar
            Console.WriteLine("end invoke\npar:"+ar.AsyncState.ToString());
        }
    }

      

 

http://www.cnblogs.com/zhaopei/p/async_one.html

http://www.cnblogs.com/myshell/archive/2010/03/23/1692059.html

http://www.cnblogs.com/rogerroddick/archive/2012/12/27/2846693.html

posted @ 2017-02-06 11:45  chy710  阅读(70)  评论(0)    收藏  举报