0

I would like to know what happens to threads which are running in background after the object gets collected by GC? Do they kill themselves or do they turn zombie process?

 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();
        }

The thread doesnt know when to stop.

Will this thread get killed once the object instance gets collected? Will GC collect this object ever since its the thread needs this.ActualDateTime?

I hope somebody had the same problem.

4

1 回答 1

4

由于线程委托已捕获this,因此不会收集对象。

由于您已使用 启动它IsBackground = true,因此当应用程序关闭时它将自行终止。

于 2013-07-30T09:23:40.207 回答