简体   繁体   中英

How can I make my countdown timer loop in C# using virtual studio?

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");
    }
} 

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.

You could take a look at the StopWatch class. You have methods such as Reset and Restart that will suit your needs.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM