Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Threading.Timer only fires once

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);
}
like image 475
ebb Avatar asked Dec 11 '11 18:12

ebb


1 Answers

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

like image 157
SLaks Avatar answered Nov 05 '22 18:11

SLaks