如何使用虚拟工作室在C#中进行倒数计时器循环?

时间:2016-11-10 08:56:29

标签: c# visual-studio

我正在闲暇时间阅读C#的一本书(programming非常新,请原谅我的经验不足)并且做了timer以便在我玩的游戏。我根本无法弄清楚如何让倒计时器自动重置,而无需手动重新运行程序。任何建议将不胜感激!非常感谢你。

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

3 个答案:

答案 0 :(得分:2)

您可以将延迟作为输入参数传递给函数。例如:

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

该列表包含有关不同延迟的信息。列表的每个元素都作为输入参数传递给方法StartProgram。该方法打印字符串并等待一段时间。

答案 1 :(得分:0)

您可以查看StopWatch课程。您有ResetRestart等方法可以满足您的需求。

答案 2 :(得分:0)

我能看到的最简单的方法是通过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");
        }
    }
} 

它会继续重启,直到您手动关闭程序