System.Threading.Timer只触发一次

本文关键字:一次 Threading Timer System | 更新日期: 2023-09-27 18:17:44

使用下面的代码,计时器只触发一次。我错过了什么?

public static List<string> Test = new List<string> { "TEST1", "TEST2" };
public static void Start()
{
    var t = new System.Threading.Timer(o =>
    {
        foreach (var item in Test)
        {
            Console.WriteLine("Say hello!"); 
        }
    }, null, 0, 1250);
}

System.Threading.Timer只触发一次

在再次触发之前,GC正在收集计时器。
您需要通过将其存储在一个字段中来保持它的活动。