C# 使用委托或函数名的线程
奇怪的问题。 使用这些代码有什么区别C# 使用委托或函数名的线程,c#,multithreading,delegates,C#,Multithreading,Delegates,奇怪的问题。 使用这些代码有什么区别 class TestThread { public void waitFunction() { // Some code like this.UpdateProgress() } public void start() { Thread thWaitingScraper = new Thread(waitFunction); // Method 1 Thread thWaitingScraper = new Thread(de
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
}
谢谢 没有功能差异,但第二种方法可能会导致类型泄漏
当编译器需要在后台创建隐式类时,会导致Typeleak。在这种情况下,由于t
waitFunction
是该类的非静态成员,编译器需要创建一个类,该类保存对该类的引用,以便使用适当的实例调用该函数。在这个类中,它创建您在第二个方法中编写的匿名方法,并将其作为线程
委托参数传递。谢谢。我只是发现另一个问题(和我的有点不同)。该问题解释了另一个与代表相关的问题: