码迷,mamicode.com
首页 > Windows程序 > 详细

C#中关于异步的三种写法

时间:2016-12-03 23:22:10      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:auto   can   val   invoke   token   timeout   void   span   adl   

1 投票

IAsyncResult ar = d1.BeginInvoke(1, 3000, null, null);

while (!ar.IsCompleted)

 

    class Program

    {

        static int TakesAWhile(int data, int ms)

        {

            Console.WriteLine("TakesAWhile started");

            Thread.Sleep(ms);

            Console.WriteLine("TakesAWhile completed");

            return ++data;

        }

 

 

        public delegate int TakesAWhileDelegate(int data, int ms);

 

        static void Main(string[] args)

        {

            TakesAWhileDelegate d1 = TakesAWhile;

            IAsyncResult ar = d1.BeginInvoke(1, 3000, null, null);

            while (!ar.IsCompleted)

            {

                // doing something else

                Console.Write(".");

                Thread.Sleep(50);

            }

            int result = d1.EndInvoke(ar);

            Console.WriteLine("result: {0}", result);

            Console.ReadLine();

        }

    }

 

 

2 等待句柄 

   class Program

    {

        static int TakesAWhile(int data, int ms)

        {

            Console.WriteLine("TakesAWhile started");

            Thread.Sleep(ms);

            Console.WriteLine("TakesAWhile completed");

            return ++data;

        }

 

 

        public delegate int TakesAWhileDelegate(int data, int ms);

 

        static void Main(string[] args)

        {

            TakesAWhileDelegate d1 = TakesAWhile;

            IAsyncResult ar = d1.BeginInvoke(1, 3000, null, null);

            int i = 0;

            while (true)

            {

                Console.Write(i++);

                Console.Write(".");

                if (ar.AsyncWaitHandle.WaitOne(50, true))

                {

                    Console.WriteLine("Can get the result now");

                    break;

                }

            }

            int result = d1.EndInvoke(ar);

            Console.WriteLine("result: {0}", result);

        }

    }

 

3 回调 pdf533  p509

    class Program

    {

        static int TakesAWhile(int data, int ms)

        {

            Console.WriteLine("TakesAWhile started");

            Thread.Sleep(ms);

            Console.WriteLine("TakesAWhile completed");

            return ++data;

        }

 

 

        public delegate int TakesAWhileDelegate(int data, int ms);

 

        static void Main(string[] args)

        {

            TakesAWhileDelegate d1 = TakesAWhile;

            IAsyncResult ar = d1.BeginInvoke(1, 3000, TakesAWhileCompleted, d1);

            for (int i = 0; i < 100; i++)

            {

                Console.Write(".");

                Thread.Sleep(50);

            }

            Console.ReadLine();

        }

 

        static void TakesAWhileCompleted(IAsyncResult ar)

        {

            if (ar == null) throw new ArgumentNullException("ar");

 

            TakesAWhileDelegate d1 = ar.AsyncState as TakesAWhileDelegate;

            Trace.Assert(d1 != null, "Invalid object type");

 

            int result = d1.EndInvoke(ar);

            Console.WriteLine("result: {0}", result);

        }

    }

 

 

通过lambda表达式来实现:

        static int TakesAWhile(int data, int ms)

        {

            Console.WriteLine("TakesAWhile started");

            Thread.Sleep(ms);

            Console.WriteLine("TakesAWhile completed");

            return ++data;

        }

 

 

        public delegate int TakesAWhileDelegate(int data, int ms);

 

        static void Main(string[] args)

        {

            TakesAWhileDelegate d1 = TakesAWhile;

            d1.BeginInvoke(1, 3000,

               ar =>

               {

                   int result = d1.EndInvoke(ar);

                   Console.WriteLine("result: {0}", result);

               },

               null);

            for (int i = 0; i < 100; i++)

            {

                Console.Write(".");

                Thread.Sleep(50);

            }

 

            Console.ReadLine();

        }

 

 

 

 

    var completed = new AutoResetEvent(false);

    var args = new SocketAsyncEventArgs();

    args.RemoteEndPoint = endpoint;

    args.Completed += OnConnectCompleted;

    args.UserToken = completed;

    socket.ConnectAsync(args);

    if (!completed.WaitOne(timeout) || !socket.Connected)

    {

        using (socket)

        {

            throw new TimeoutException("Could not connect to " + endpoint);

        }

    } 

C#中关于异步的三种写法

标签:auto   can   val   invoke   token   timeout   void   span   adl   

原文地址:http://www.cnblogs.com/shi5588/p/6129618.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!