gpt4 book ai didi

c# - 包含永无止境任务的对象会被垃圾回收吗?

转载 作者:行者123 更新时间:2023-11-30 17:37:25 24 4
gpt4 key购买 nike

<分区>

我有以下简单的类:

public sealed class TimeStampTestViewModel : ObservableObject
{
private CancellationTokenSource cancellationSource;

public TimeStampTestViewModel()
{
this.cancellationSource = new CancellationTokenSource();
PeriodicTimeStampChanged(cancellationSource.Token);
}

public string TimeStamp
{
get
{
return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
}

private async void PeriodicTimeStampChanged(CancellationToken token)
{
while (!token.IsCancellationRequested)
{
RaisePropertyChanged("TimeStamp");
await Task.Delay(500, token);
}
}
}

这个类的实例会被垃圾回收吗?或者无限运行任务的存在会阻止垃圾收集吗?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com