1

I have a Console Application with the following main program:

    static void Main(string[] args)
    {
        var timer = new System.Threading.Timer(
         e =>
            {
            //some code here
        },
        null,
        TimeSpan.Zero,
        TimeSpan.FromMinutes(1));


        var backupTimer = new System.Threading.Timer(
         e =>
         {
             //some code here

         },
        null,
        TimeSpan.Zero,
        TimeSpan.FromHours(24));

        Console.ReadLine();

    }

The problem is that in debug mode it works fine and call methods in both timers in right period and if enter something in console the program ends working (Console.ReadLine() is for that) , but when i'm running program in Release mode both timers called only once (the first time) and then program just waiting until i enter something.

How to fix the problem , so i can compile a standalone program working properly?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
s1ddok
  • 4,615
  • 1
  • 18
  • 31

2 Answers2

3

As @SriramSakthivel suggested you have to keep a reference to timer as a field, otherwise garbage collector eats your timers. so here is solution :

    private static System.Threading.Timer timer;
    private static System.Threading.Timer backupTimer;

    static void Main(string[] args)
    {

        timer = new System.Threading.Timer(
         e =>
            {
            //something
        },
        null,
        TimeSpan.Zero,
        TimeSpan.FromMinutes(1));


        backupTimer = new System.Threading.Timer(
         e =>
         {
             //something

         },
        null,
        TimeSpan.Zero,
        TimeSpan.FromHours(24));

        Console.ReadLine();

    }
s1ddok
  • 4,615
  • 1
  • 18
  • 31
0

Hope the following solution helps!

Problem:

The following code always works as expected by showing the current clock time.

 class Program
 {
        static void TimerProcedure(object param)
        {
            Console.Clear();
            Console.WriteLine(DateTime.Now.TimeOfDay);

            GC.Collect();

        }

        static void Main(string[] args)
        {
            Console.Title = "Desktop Clock";
            Console.SetWindowSize(20, 2);
            Timer timer = new Timer(TimerProcedure, null,
                TimeSpan.Zero, TimeSpan.FromSeconds(1));
            Console.ReadLine();
        }
    }

However, if you run the same code in release mode, it shows current time only for the first time but it never updates after that.

Solution

A simple tweak by adding GC.KeepAlive(object) will make it work in Release mode also as expected. Refer to code below.

class Program
    {
        static void TimerProcedure(object param)
        {
            Console.Clear();
            Console.WriteLine(DateTime.Now.TimeOfDay);
            #region Hidden
            GC.Collect();
            #endregion
        }

        static void Main(string[] args)
        {
            Console.Title = "Desktop Clock";
            Console.SetWindowSize(20, 2);
            Timer timer = new Timer(TimerProcedure, null,
                TimeSpan.Zero, TimeSpan.FromSeconds(1));
            Console.ReadLine();
            GC.KeepAlive(timer);
        }
    }

Alternate solution: is making timer a class level static variable