Mode77
Mode77

Reputation: 1041

Halting program execution until a timer stops

I want to produce some text in a console window for a certain amount of time before clearing it and showing more text. I thought the following code would do that, but instead, after the timer expires, the console is cleared but no other text appears.

Is this the correct way to halt program execution until a timer expires?

static void Main (string [] args)
{
    const int PEEK_TIME = 5;
    const int SECOND = 1000;

    Timer peekTimer = new Timer (SECOND * PEEK_TIME);
    peekTimer.Elapsed += onTimerTick;
    peekTimer.AutoReset = false;

    peekTimer.Start ();
    Console.WriteLine ("Timer started...");
    while (peekTimer.Enabled) { }
    Console.WriteLine ("Timer done.");
    Console.ReadLine ();
}

static void onTimerTick (Object source, System.Timers.ElapsedEventArgs e)
{
    Console.Clear ();
}

Upvotes: 0

Views: 187

Answers (4)

EvgenieT
EvgenieT

Reputation: 251

In my game I use this approach:

public static Timer peekTimer = new Timer (SECOND * PEEK_TIME);

static void Main (string [] args)
{
const int PEEK_TIME = 5;
const int SECOND = 1000;    

Console.WriteLine ("Timer started...");
peekTimer.Interval = new TimeSpan(0, 0, 0, 0, SECOND * PEEK_TIME);
peekTimer.Tick += new EventHandler(onTimerTick);
peekTimer.Start();

}

static void onTimerTick (Object source, System.Timers.ElapsedEventArgs e)
{
peekTimer.Stop();
Console.Clear ();
Console.WriteLine ("Timer done.");
Console.ReadLine ();
}

Upvotes: 0

Rufus L
Rufus L

Reputation: 37060

In your onTimerTick event, you need to stop the timer (or set peekTimer.Enabled == false) if you want the while loop to exit:

static void onTimerTick (Object source, System.Timers.ElapsedEventArgs e)
{
    Console.Clear ();
    peekTimer.Stop();
}

In order to do this, you'll have to declare the timer at a wider scope so you can access it from your Main method and from the Tick event handler:

namespace ConsoleApplication
{
    timer peekTimer;

    public class Program
    {

In answer to your larger question, this is not the best way to do it. If you're just going to sit and do nothing for a specific amount of time, you might as well just put the thread to sleep rather than spinning around the whole time:

Console.WriteLine("Waiting 5 seconds...");
Thread.Sleep(TimeSpan.FromSeconds(5));
Console.WriteLine("...Time's up!!");

Upvotes: 0

abydal
abydal

Reputation: 388

Here is codesample using the Thread.Sleep function.

using System;
using System.Threading;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main()
        {
            Console.WriteLine("Timer Start!");

            Thread.Sleep(5000);
            Console.Clear();
            Console.WriteLine("End");

            Console.ReadKey();
        }
    }
}

Upvotes: 2

eocron
eocron

Reputation: 7546

You don't need timer for this:

static void Main (string [] args)
{
    const int PEEK_TIME = 5;
    const int SECOND = 1000;

    var task = Task.Run(()=> 
        {
            Console.WriteLine ("Work started...");
            Thread.Sleep(PEEK_TIME*SECOND);
            Console.Clear();
        });
    task.Wait();
    Console.WriteLine ("Work done.");
    Console.ReadLine ();
}

Upvotes: 0

Related Questions