C# 后台无限线程与GC
我想知道在GC收集对象之后,后台运行的线程会发生什么情况?他们是自杀还是变成僵尸C# 后台无限线程与GC,c#,C#,我想知道在GC收集对象之后,后台运行的线程会发生什么情况?他们是自杀还是变成僵尸 private Thread currentDateTimer; private DateTime ActualDateTime; if (this.currentDateTimer == null) { this.currentDateTimer = new Thread(() => { while (t
private Thread currentDateTimer;
private DateTime ActualDateTime;
if (this.currentDateTimer == null)
{
this.currentDateTimer = new Thread(() =>
{
while (true)
{
this.ActualDateTime = DateTime.Now;
Thread.Sleep(60 * 1000 - (DateTime.Now.Second * 1000 + DateTime.Now.Millisecond));
}
}) { IsBackground = true };
this.currentDateTimer.Start();
}
线程不知道何时停止
一旦对象实例被收集,这个线程会被杀死吗?自从线程需要this.ActualDateTime以来,GC是否会收集此对象
我希望有人也有同样的问题。因为线程委托已经捕获了这个,对象将不会被收集
由于您已使用
IsBackground=true
启动它,因此当应用程序关闭时,它将自动终止。由于线程委托已捕获此
,因此将不会收集对象
由于您是以
IsBackground=true
启动它的,因此当应用程序关闭时,它将自动终止。我是否理解正确,即使我没有其他东西指向对象,对象也不会被收集?正如@Lasse所说,线程的委托仍将指向对象,因此它不会被收集。有趣。这让我想起了actionscript 3中的GC,如果你在本地范围内声明tweens,tweens会突然停止工作。我说得对吗?即使我没有其他东西指向对象,对象也不会被收集?正如@Lasse所说,线程的委托仍然会指向对象,因此它不会被收集。有趣。这让我想起了ActionScript3中的GC,如果在本地范围内声明tweens,tweens将突然不再工作