Reputation: 1
I am reading a book on C#
in my spare time (Very new to programming
, pardon my inexperience) and have made it as far as making a timer
for afking on a game I play. I simply cant figure out how to make the countdown timer reset itself without having to re run the program manually. Any suggestions would be greatly appreciated! Thank you very much.
public class Afktimer
{
public void StartProgram()
{
SnapsEngine.SetTitleString("Afk Timer");
SnapsEngine.Delay(3.0);
SnapsEngine.SpeakString("Twenty minutes remaining");
SnapsEngine.Delay(600.0);
SnapsEngine.SpeakString("Ten minutes remaining");
SnapsEngine.Delay(300.0);
SnapsEngine.SpeakString("Five minutes remaining");
SnapsEngine.Delay(240.0);
SnapsEngine.SpeakString("One minute remaining");
SnapsEngine.Delay(60.0);
SnapsEngine.SpeakString("Timer resetting");
}
}
Upvotes: 0
Views: 504
Reputation: 1
easiest way I can see is to just run it through a while statement so it would be something like
public class Afktimer
{
public void StartProgram()
{
while (true)
{
SnapsEngine.SetTitleString("Afk Timer");
SnapsEngine.Delay(3.0);
SnapsEngine.SpeakString("Twenty minutes remaining");
SnapsEngine.Delay(600.0);
SnapsEngine.SpeakString("Ten minutes remaining");
SnapsEngine.Delay(300.0);
SnapsEngine.SpeakString("Five minutes remaining");
SnapsEngine.Delay(240.0);
SnapsEngine.SpeakString("One minute remaining");
SnapsEngine.Delay(60.0);
SnapsEngine.SpeakString("Timer resetting");
}
}
}
it'll just keep restart it until you close the program manually
Upvotes: 0
Reputation: 3416
You can pass the delay as input parameter to a function. For example:
using System.Collections.Generic;
using System.Linq;
namespace Test
{
public class Program
{
private static void Main(string[] args)
{
var delays = new List<int> { 600, 300, 240, 60 }; // List with delays.
delays.ForEach(delay => StartProgram(delay)); // For each element in delays call StartProgram.
}
public static void StartProgram(int delay)
{
SnapsEngine.SpeakString("{0} minutes remaining", delay / 60);
SnapsEngine.Delay(delay);
}
}
}
The list contains your information about the different delays. Each element of the list get passed to the method StartProgram
as input parameter. The method print the string and wait for the amount of time.
Upvotes: 2
Reputation: 18769
You could take a look at the StopWatch
class. You have methods such as Reset
and Restart
that will suit your needs.
Upvotes: 0