3
\$\begingroup\$

Just a simple timer application for review. Stop and start the timer from the command line.

The plan is to use this in a poker application to fold out a player if they don't act after a time period (call the clock on them). I was trying to use async await but it did not seem like the right tool for this.

MyTimer myTimer = new MyTimer();
while (true)
{
 string readLine = Console.ReadLine();
 if (readLine == "stop")
 {
 myTimer.TimerStop();
 }
 else if (readLine == "start")
 {
 myTimer.TimerStart();
 }
 else if (readLine == "q")
 {
 Environment.Exit(0);
 }
}

.

public class MyTimer
{
 private System.Timers.Timer aTimer = new System.Timers.Timer();
 private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
 {
 Debug.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
 Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
 }
 public void TimerStop()
 {
 aTimer.Enabled = false;
 Console.WriteLine("aTimer.Enabled = false");
 }
 public void TimerStart()
 {
 aTimer.Enabled = true;
 Console.WriteLine("aTimer.Enabled = true");
 }
 public MyTimer()
 {
 aTimer.Interval = 2000;
 // Hook up the Elapsed event for the timer. 
 aTimer.Elapsed += OnTimedEvent;
 // Have the timer fire repeated events (true is the default)
 aTimer.AutoReset = true;
 // Start the timer
 aTimer.Enabled = true;
 Debug.WriteLine("aTimer.Enabled");
 Console.WriteLine("aTimer.Enabled");
 }
}
asked Apr 3, 2018 at 14:57
\$\endgroup\$
1
  • 2
    \$\begingroup\$ VTC can you tell me what else you are looking for? \$\endgroup\$ Commented Apr 3, 2018 at 16:11

1 Answer 1

3
\$\begingroup\$

Why aren't you inheriting from Timer it's not a sealed class and is disposable (and I believe must be disposed).

public class PokerTimer : Timer
{
 public PokerTimer() : this(2000)
 {
 }
 public PokerTimer(double interval) : base(interval)
 {
 AutoReset = true;
 Elapsed += OnTimedEvent;
 }
 protected override void Dispose(bool disposing)
 {
 //clean up anything you need to here or remove this override.
 base.Dispose(disposing);
 }
}

All that being said, you said this is for a game where you want to time out players so you might want to look a bit into game loops and managing state in such a loop.

answered Apr 10, 2018 at 2:42
\$\endgroup\$
1
  • \$\begingroup\$ I will look into game loops. \$\endgroup\$ Commented Apr 10, 2018 at 5:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.