0

Inside a console app I have the following code:

    private static System.Timers.Timer TaskTimer;
    static void Main(string[] args)
    {
        //run each minute
        Func<double> getNextMinute = () => { return (DateTime.Now.RoundUp(TimeSpan.FromMinutes(1)) - DateTime.Now).TotalMilliseconds; };
        TaskTimer = new System.Timers.Timer(getNextMinute());
        TaskTimer.AutoReset = false;
        TaskTimer.Elapsed += (sender, e) =>
        {
            TaskTimer.Interval = getNextMinute();
            TaskTimerCallback();
        };
        TaskTimer.Start();

        bool exit = false;
        while (!exit)
        {
            exit = (command == "exit");
        }
    }

If I check the timers GC generation, it's always 0.

Is there any chance that the TaskTimerObject will get garbage collected before a tick?

adrianvlupu
  • 4,188
  • 3
  • 23
  • 29

1 Answers1

0

no, the GC won't collect the TaskTimerObject.

the field TaskTimer is a Root Referance which points to TaskTimerObject.

Community
  • 1
  • 1
Old Fox
  • 8,629
  • 4
  • 34
  • 52