1

At least for me, this is the perfect example of using System.Timers.Timer. The only problem is that it will not work if I eliminate Console.ReadLine(). In my case, I just want to display a message after 5 seconds have passed and then the console will close. That's it.

So let's say that I want to display simple message Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime) without any user interaction, how would I be able to do that? In other words, when I hit F5, I'll see the blank console window, in 5 seconds I will see the message, and then the console will go away.

Here's the code from MSDN:

using System;
using System.Timers;

public class Example
{
    private static Timer aTimer;

    public static void Main()
    {
        // Create a timer with a two second interval.
        aTimer = new System.Timers.Timer(5000);
        // Hook up the Elapsed event for the timer. 
        aTimer.Elapsed += OnTimedEvent;
        aTimer.Enabled = true;

        Console.WriteLine("Press the Enter key to exit the program... ");
        Console.ReadLine();
        Console.WriteLine("Terminating the application...");
    }

    private static void OnTimedEvent(Object source, ElapsedEventArgs e)
    {
        Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
    }
}
Joe_Hendricks
  • 746
  • 4
  • 16
  • Thread.Sleep not good enough for you? – Mike Miller Sep 09 '14 at 15:27
  • @MikeMiller OP [already tried](http://stackoverflow.com/questions/25747006/replace-thread-sleep-with-system-threading-timer) and something did not work (not exactly clear what so). At least with this one the issue is straight forward. – Alexei Levenkov Sep 09 '14 at 15:32

3 Answers3

4
using System;
using System.Threading;
using System.Timers;
using Timer = System.Timers.Timer;

private static Timer aTimer;
private static ManualResetEventSlim ev = new ManualResetEventSlim(false);

public static void Main()
{
    // Create a timer with a two second interval.
    aTimer = new System.Timers.Timer(5000);
    // Hook up the Elapsed event for the timer. 
    aTimer.Elapsed += OnTimedEvent;
    aTimer.Enabled = true;
    ev.Wait();
}

private static void OnTimedEvent(Object source, ElapsedEventArgs e)
{
    Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
    ev.Set();
}
Viktor Arsanov
  • 1,285
  • 1
  • 9
  • 17
2

There are several different ways.

Here is one example:

using System;
using System.Timers;

public class Example
{
   private static Timer aTimer;
   private static bool delayComplete = false;

   public static void Main()
   {
      // Create a timer with a two second interval.
      aTimer = new System.Timers.Timer(5000);
      // Hook up the Elapsed event for the timer. 
      aTimer.Elapsed += OnTimedEvent;
      aTimer.Enabled = true;

      while (!delayComplete)
      {
         System.Threading.Thread.Sleep(100);
      }
   }

   private static void OnTimedEvent(Object source, ElapsedEventArgs e)
   {
      Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
      delayComplete = true;
   }
}
LVBen
  • 2,041
  • 13
  • 27
2

You application will exit when you main thread terminates. That's the thread that executes Main(). You timer will fire on a different thread. So basically what you need to do if you don't want to do Console.ReadLine() is Thread.Sleep(5000) where 5000 is the number of milliseconds the thread will sleep for. This will make your main thread wait until the timer fires.

Zeus82
  • 6,065
  • 9
  • 53
  • 77