• c# 多线程简化


    编译器自动推断出ParameterizedThreadStart委托,因为Go方法接收一个单独的object参数,就像这样写:

    1

    2

    Thread t = new Thread (new ParameterizedThreadStart (Go));

    t.Start (true);

    ParameterizedThreadStart的特性是在使用之前我们必需对我们想要的类型(这里是bool)进行装箱操作,并且它只能接收一个参数。

      一个替代方案是使用一个匿名方法调用一个普通的方法如下:

    1

    2

    3

    4

    5

    static void Main() {

      Thread t = new Thread (delegate() { WriteText ("Hello"); });

      t.Start();

    }

    static void WriteText (string text) { Console.WriteLine (text); }

      优点是目标方法(这里是WriteText),可以接收任意数量的参数,并且没有装箱操作。不过这需要将一个外部变量放入到匿名方法中,向下面的一样:

    1

    2

    3

    4

    5

    6

    7

    static void Main() {

      string text = "Before";

      Thread t = new Thread (delegate() { WriteText (text); });

      text = "After";

      t.Start();

    }

    static void WriteText (string text) { Console.WriteLine (text); }

  • 相关阅读:
    结束也是开始
    21.1 LogMiner
    20.4 使用FLASHBACK DATABASE 恢复数据库到先前状态
    结束也是开始
    21.2 DBVERIFY
    21.3 DBNEWID
    20.2 使用FlashBack Table 恢复表到先前状态
    21.2 DBVERIFY
    21.3 DBNEWID
    20.3 使用FLASHBACK TABLE 恢复被删除表
  • 原文地址:https://www.cnblogs.com/embaobao/p/10720833.html
Copyright © 2020-2023  润新知