假设有以下情况...
public MyClass
{
public void MyClass()
{
var myWorkerClass = new MyWorkerClass();
myWorkerClass.DoSomething();
// and exit quickly
}
}
public MyWorkerClass()
{
public void DoSomething()
{
ThreadPool.QueueUserWorkItem(() =>
{
SomeLongRunningProcess();
});
}
public static void SomeLongRunningProcess()
{
// Something that takes a long time
}
}
MyWorkerClass和MyClass何时变得可以进行垃圾回收呢?
我认为MyWorkClass.DoSomething()中的匿名函数将存储在MyWorkerClass的局部变量声明空间中。这将足以保留对它们的引用,直到线程完成。这个想法正确吗?
MyWorkerClass
实例。 - spender