我正在嘗試使「簡單」計時器從15分鐘到0秒。我在15分鐘內用了900秒。當我運行該程序時,它運行良好,但繼續進行否定。我仍然是C#的新手。我希望代碼在0處停下來並運行警報以吸引別人的注意力。以下是我迄今不可阻擋的計時器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
namespace GBS_GI_Timer
{
public class Program
{
public static int t = 2;
public static void Main()
{
System.Timers.Timer aTimer = new System.Timers.Timer();
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = 1000;
aTimer.Enabled = true;
//Console.WriteLine("Press the Enter key to exit the program.");
Console.ReadLine();
//GC.KeepAlive(aTimer);
if (t == 0)
aTimer.Stop();
}
public static void OnTimedEvent(object source, ElapsedEventArgs e)
{
//TimeSpan timeRemaining = TimeSpan.FromSeconds(t);
Console.WriteLine("Time remianing..{0}", t);
t--;
if (t == 0)
{
Console.WriteLine("\a");
Console.WriteLine("Time to check their vitals, again!");
Console.WriteLine("Press any key to exit...");
}
// Console.ReadKey();
Console.ReadLine();
}
}
}
我很快把它放在一起添加到一個演示,以顯示它將執行並且它將發送一個警報。可能爲什麼我的邏輯至少可以說是殘暴的。 –