• C# delegate方法invoke和beginInvoke的区别


    class Program
        {
            delegate void test();
            static void Main(string[] args)
            {
                test ts = new test(TestDelegate);
                ts.BeginInvoke(null,null);             //使用到委托的beginInvoke方法
                Console.WriteLine("hello");
            }

            internal static void TestDelegate()
            {
                Thread.Sleep(5000);
            }

        }

    上面程序代码中使用到begininvoke方法,此时控制台会立刻输出hello字符,然后结束主程序运行。由此可知beginInvoke是在主线程之外,另起了一个线程来运行其所需的代码,可以理解为异步调用。

    再看下面这点程序

        class Program
        {
            delegate void test();
            static void Main(string[] args)
            {
                test ts = new test(TestDelegate);
                ts.Invoke();     //使用到委托的invoke方法
                Console.WriteLine("hello");
            }

            internal static void TestDelegate()
            {
                Thread.Sleep(5000);
            }

        }

    唯一区别就是使用到了 invoke方法,此时控制台会等待5秒,然后才输出hello字符。由此可知invoke是使用主线程运行其代码的,并没有另起线程,可以理解为同步调用。

  • 相关阅读:
    shell脚本快速配置yum源
    RAID 5+1
    RAID 10
    TFTP
    CentOS7 初始化脚本 2.0
    CentOS7 初始化脚本 1.0
    Tomcat CGI 轻松打造 Web 服务
    Python 变量类型 —— type() 函数和 isinstance() 函数
    Python源码换行
    RFC 文档搜索与阅读
  • 原文地址:https://www.cnblogs.com/itjeff/p/14608263.html
Copyright © 2020-2023  润新知