0% found this document useful (0 votes)
129 views

My Timer

This document defines a CustomTimer class that uses a System.Timers.Timer to count down from a specified duration. The timer changes the console text color as the duration decreases. When the duration reaches 0, it stops the timer and displays a message that time is up. The class contains methods to start the timer counting down from a given duration, update the console output as the duration decreases each tick, and stop the timer.

Uploaded by

Matthew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views

My Timer

This document defines a CustomTimer class that uses a System.Timers.Timer to count down from a specified duration. The timer changes the console text color as the duration decreases. When the duration reaches 0, it stops the timer and displays a message that time is up. The class contains methods to start the timer counting down from a given duration, update the console output as the duration decreases each tick, and stop the timer.

Uploaded by

Matthew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

using

using
using
using
using
using

System;
System.Collections.Generic;
System.Linq;
System.Text;
System.Threading.Tasks;
System.Timers;

namespace CountdownTimer
{
class CustomTimer
{
Timer timer1;
int duration;
public CustomTimer(int tickRate)
{
timer1 = new Timer(tickRate);
Console.WriteLine("\nYou've just created a timer!\n");
timer1.Elapsed += new ElapsedEventHandler(MyTimer_Elapsed);
} // end Constructor
private void MyTimer_Elapsed(object sender, ElapsedEventArgs e)
{
if (duration >= 0)
{
ClearCurrentConsoleLine();
if (duration >= 25)
Console.ForegroundColor = ConsoleColor.Blue;
else if (duration >= 20)
Console.ForegroundColor = ConsoleColor.Cyan;
else if (duration >= 15)
Console.ForegroundColor = ConsoleColor.DarkGray;
else if (duration >= 10)
Console.ForegroundColor = ConsoleColor.DarkRed;
else if (duration >= 5)
Console.ForegroundColor = ConsoleColor.Yellow;
else
Console.ForegroundColor = ConsoleColor.Magenta;
Console.Write("{0}", duration);
duration--;
} // end if
else
StopMyTimer();
} // end MyTimer_Elapsed()
private void StopMyTimer()
{
timer1.Stop();
Console.WriteLine("\n\nYour time is up!!!");
} // end StopMyTimer()

public void StartMyTimer(int newDuration)


{
duration = newDuration;
Console.WriteLine("\n\nCounting down from {0}!\n", duration);
timer1.Start();
} // end StartMyTimer()
private void ClearCurrentConsoleLine()
{
int currentLineCursor = Console.CursorTop;
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, currentLineCursor);
} // end ClearCurrentConsoleLine()
} // end custom timer class
} // end namespace

You might also like