简体   繁体   English

如何使用Virtual Studio在C#中使倒数计时器循环?

[英]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. 我在业余时间正在读一本有关C#的书(这是programming新手,请原谅我的经验),而且它还为我在玩的游戏中伪造了一个timer 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. 列表中的每个元素都作为输入参数传递给StartProgram方法。 The method print the string and wait for the amount of time. 该方法打印字符串并等待一段时间。

You could take a look at the StopWatch class. 您可以看一下StopWatch类。 You have methods such as Reset and Restart that will suit your needs. 您有适合您需要的方法,例如“ Reset和“ Restart

easiest way I can see is to just run it through a while statement so it would be something like 我看到的最简单的方法是只通过while语句运行它,所以它就像

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 它会一直重启,直到您手动关闭程序为止

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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