System.Threading.Timer仅触发一次 [英] System.Threading.Timer only fires once

查看:90
本文介绍了System.Threading.Timer仅触发一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的代码,计时器仅触发一次.我想念什么?

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

推荐答案

GC在再次触发之前正在收集计时器.
您需要通过将其存储在字段中来保持活动状态.

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

这篇关于System.Threading.Timer仅触发一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆