zoukankan      html  css  js  c++  java
  • C# 委托的同步调用和异步调用

      委托的Invoke方法用来进行同步调用。同步调用也可以叫阻塞调用,它将阻塞当前线程,然后执行调用,调用完毕后再继续向下进行。

      同步调用的例子:

    namespace ConsoleApplication1
    {
        public class MyClass
        {
            public static void Main(string[] args)
            {
                Invoke.SyncInvoke();
            }
        }
        public delegate int AddHandler(int a, int b);
        public class Invoke
        {
            public static int Add(int a, int b)
            {
                Console.WriteLine("Computing {0} + {1} ...", a, b);
                Thread.Sleep(3000);
                Console.WriteLine("Computing Complete.");
                return a + b;
            }
    
            /// <summary>
            /// 同步调用
            /// </summary>
            public static void SyncInvoke()
            {
                Console.WriteLine("**********Sync Invoke Test**************");
                AddHandler handler = new AddHandler(Add);
                int result = handler.Invoke(1, 2);
                Console.WriteLine("Do other work  ... ... ...");
                Console.WriteLine(result);
                Console.ReadLine();
            }
        }
    }
    运行结果:



    同步调用会阻塞线程,如果是要调用一项繁重的工作(如大量IO操作),可能会让程序停顿很长时间,造成糟糕
      的用户体验,这时候异步调用就很有必要了。
      异步调用不阻塞线程,而是把调用塞到线程池中,程序主线程或UI线程可以继续执行。
      委托的异步调用通过BeginInvoke和EndInvoke来实现。

    异步调用:

    namespace ConsoleApplication1
    {
        public class MyClass
        {
            public static void Main(string[] args)
            {
                Invoke.AsyncInvoke();
            }
        }
        public delegate int AddHandler(int a, int b);
    
        public class Invoke
        {
            public static int Add(int a, int b)
            {
                Console.WriteLine("Computing {0} + {1} ...", a, b);
                Thread.Sleep(3000);
                Console.WriteLine("Computing Complete.");
                return a + b;
            }/// <summary>
            /// 异步调用
            /// </summary>
            public static void AsyncInvoke()
            {
                Console.WriteLine("**********Async Invoke Test**************");
                AddHandler handler = new AddHandler(Add);
                IAsyncResult result = handler.BeginInvoke(1, 2, null, null);
                Console.WriteLine("Do other work... ... ...");
                Console.WriteLine(handler.EndInvoke(result));
                Console.ReadLine();
            }        
        }
    }
    运行结果:


    可以看到,主线程并没有等待,而是直接向下运行了。
      但是问题依然存在,当主线程运行到EndInvoke时,如果这时调用没有结束(这种情况很可能出现),这时为了等待调用结果,线程依旧会被阻塞。
      解决的办法是用回调函数,当调用结束时会自动调用回调函数

    回调异步:

    namespace ConsoleApplication1
    {
        public class MyClass
        {
            public static void Main(string[] args)
            {
                Invoke.AsyncAutoCallback();
            }
        }
        public delegate int AddHandler(int a, int b);
    
        public class Invoke
        {
            public static int Add(int a, int b)
            {
                Console.WriteLine("Computing {0} + {1} ...", a, b);
                Thread.Sleep(3000);
                Console.WriteLine("Computing Complete.");
                return a + b;
            }  /// <summary>
            /// 异步回调(回调函数)
            /// </summary>
            public static void AsyncAutoCallback()
            {
                Console.WriteLine("**********Async Invoke Test**************");
                AddHandler handler = new AddHandler(Add);
                IAsyncResult result = handler.BeginInvoke(1, 2, new AsyncCallback(AddComplete), "AsycState:OK");
                Console.WriteLine("Do other work... ... ...");
                Console.ReadLine();
            }
    
            static void AddComplete(IAsyncResult result)
            {
                AddHandler handler = (AddHandler)((AsyncResult)result).AsyncDelegate;
                Console.WriteLine(handler.EndInvoke(result));
                Console.WriteLine(result.AsyncState);
            }
        }
    }

    ref:http://kb.cnblogs.com/page/50132/

  • 相关阅读:
    asp.net2.0 Theme and Skin
    Microsoft Exchange Server 2010 介绍
    Visual Studio 2010 Team System 动手实验室
    WCF中的消息契约
    Windows Workflow Foundation实验01——Windows Workflow Foundation 快速入门(练习二)
    C#中Brush、Color、String相互转换
    VS自动生成有参构造函数并自动依赖注入插件
    C#集合已修改:可能无法执行枚举操作
    Docker安装后启动不了,报“参考的对象类型不支持尝试的操作”
    windows下安装Docker超全图文教程
  • 原文地址:https://www.cnblogs.com/jes_shaw/p/2955771.html
Copyright © 2011-2022 走看看