System.Threading.Timer only fires once

吃可爱长大的小学妹 提交于 2021-02-04 22:24:10

问题


Using the code below, the timer only fires once. What am I missing?

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

回答1:


The timer is being collected by the GC before it fires again.
You need to keep it alive by storing it in a field.



来源:https://stackoverflow.com/questions/8466475/system-threading-timer-only-fires-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!