【问题标题】:Thread using delegate or function name使用委托或函数名称的线程
【发布时间】:2014-04-04 22:51:35
【问题描述】:

奇怪的问题。 使用这些代码有什么区别?

class TestThread {

public void waitFunction() {
   // Some code like this.UpdateProgress()
}

public void start() {
    Thread thWaitingScraper = new Thread(waitFunction);   // Method 1

    Thread thWaitingScraper = new Thread(delegate() { waitFunction(); });   // Method 2
}

谢谢!

【问题讨论】:

    标签: c# multithreading delegates


    【解决方案1】:

    没有功能上的区别,但是第二种方法会导致类型泄漏。

    Typeleak 是在编译器需要在后台创建隐式类时引起的。在这种情况下,因为 twaitFunction 是类的非静态成员,编译器需要创建一个包含对 this 类的引用的类,以便可以使用适当的实例调用函数。在此类中,它会创建您在第二个方法中编写的匿名方法,并将其作为Thread 委托参数传递。

    【讨论】:

    猜你喜欢
    • 2013-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多