0

I'm making a simple game on console for practice that requires a time limit each round and I've encountered a problem with trying to make it so I can use timer more than once. I have this:

class Program
{
    static Timer timer = new Timer(1000);
    static int t = 10;

    static void Main(string[] args)
    {
        timer.Elapsed += Timer_Elapsed1;
        timer.Start();
        Console.ReadLine();
        t = 10;
        timer.Start();
        Console.ReadLine();
    }

My thoughts were that the 2nd timer.Start() would get the same result as the first, but nothing happens.

private static void Timer_Elapsed1(object sender, ElapsedEventArgs e)
{
    t--;
    Console.WriteLine("Hello!");

    if (t == 0)
    {
        Console.WriteLine("Goodbye!");
        timer.Stop();
    }
}

Why is the second timer.Start() not doing anything? How do I make it so I can use timer.Start() again and it will do the same thing as the first time? I'm using System.Timers

NVM IT DOES WORK, IM JUST DUMB LOL

Yummy275
  • 293
  • 1
  • 11
  • Should your first timer not keep elapsing after 1000 ms? From my previous work with timers, once it is started, it will keep firing at the interval. – ti034 Oct 18 '18 at 15:07
  • Which timer are you using? There are at least 3 `Timer` classes in .Net – Ian Oct 18 '18 at 15:11
  • @Ian I'm using System.Timers – Yummy275 Oct 18 '18 at 15:12
  • 'My thoughts were that the 2nd timer.Start() would get the same result as the first' what result are you referring to ? Why do you think you need the second call to timer.Start() ? – auburg Oct 18 '18 at 15:14
  • The `System.Timers` timer periodically raises the `TimerElapsed` event at a given interval. Calling `Start` sets a boolean to true, saying that it will raise the event, calling `Stop` sets the boolean to false, saying that it won't raise the event. Calling `Start` the second time does nothing because it's telling the time to raise the event, but it's already doing that. – St. Pat Oct 18 '18 at 15:18
  • @auburg the result of the first start, being the 10 "Hello!" And 1 "Goodbye!". I want the timer to stop or pause until it needs to tick again , I figured I'd have to start it up again once I need it again. – Yummy275 Oct 18 '18 at 15:19
  • @Yummy275 Just to be sure, are you pressing Enter after the first Goodbye? – St. Pat Oct 18 '18 at 15:27
  • I've ran your code and it seems to work as intended i.e. if you wait for 10 seconds and let it print Goodbye and then press a key the second Start() call triggers the timer countdown again. Isn't this what you're expecting ? – auburg Oct 18 '18 at 15:29
  • OMG IM DUMB . You're right it does work , I was smashing the enter button which caused it to end. I'm sorry for wasting your guys time ! Embarrassing haha – Yummy275 Oct 18 '18 at 15:33
  • No worries, glad we could get it worked out – St. Pat Oct 18 '18 at 15:36
  • One tip - don't forget to call timer.Dispose() – auburg Oct 18 '18 at 15:39
  • @auburg I've read that it's important to do that ,but I'm curious as to when i should call it. Should call it when I'm all done using timer , or should I add it after timer.Stop()? Oh and I forgot to say thank you! – Yummy275 Oct 18 '18 at 15:49
  • When you're done using it – auburg Oct 19 '18 at 07:42

2 Answers2

0

Try stopping or disabling the first timer before trying to start a second time.

C# - how do you stop a timer?

ti034
  • 191
  • 2
  • 13
0

I fell compelled to give you some disclaimers:

First, console is not the right Environment for Game Development. Neither is any of the GUI techs. For games you got XNA (pretty dated) and with .NET Core a bunch of new Options. The important thing is that you have a Game Loop of some form. Or at least imitate one.

Secondly, I am unsure how well most Timers work in console apps. Most of them use callbacks wich usually require a MessageQueue - wich is a GUI feature. I guess you could try a Multithreading time, but then you have to relearn everything if you leave Console applications.

As for your code: I am unsure when the Timer tick happens since you specified no interval. But I guess either:

  • never
  • after the 2nd timer start
Christopher
  • 9,634
  • 2
  • 17
  • 31