如何在一天结束时自动重置计时器,如何显示上次执行的时间和日期? 该计划是 -
namespace Time_Writer
{
class Program
{
static int count = 1;
static double seconds;
static int total = 10000;
private static System.Timers.Timer aTimer;
static void Main(string[] args)
{
ReadCountFromFile();
aTimer = new System.Timers.Timer();
aTimer.Elapsed +=new System.Timers.ElapsedEventHandler(aTimer_Elapsed);
aTimer.Interval = 5000;
aTimer.Enabled = true;
Console.WriteLine("Press Enter To Exit The Program\n");
Console.ReadLine();
AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
}
private static void ReadCountFromFile()
{
try
{
if (File.Exists(".\\mynumber.dat"))
{
using (var file = File.Open(".\\mynumber.dat", FileMode.Open))
{
byte[] bytes = new byte[4];
file.Read(bytes, 0, 4);
count = BitConverter.ToInt32(bytes, 0);
total = total - count;
Console.WriteLine("Total count left is = {0}", total);
Console.WriteLine("Limit = 10000");
Console.WriteLine("Count = {0}", count);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Problem reading file.");
}
}
static void CurrentDomain_ProcessExit(Object sender, EventArgs e)
{
using (var file = File.Open(".\\mynumber.dat", FileMode.OpenOrCreate))
{
var buffer = BitConverter.GetBytes(count);
file.Write(buffer, 0, buffer.Length);
}
}
private static void aTimer_Elapsed(object source, ElapsedEventArgs e)
{
Console.WriteLine("Name is Yap {0}", e.SignalTime);
seconds += 5;
count += 1;
if (count>10000 || seconds == 86400)
{
aTimer.Enabled = false;
Console.WriteLine("\n\nTimer is off at {0}\n\n", e.SignalTime.TimeOfDay.ToString());
}
}
}
}
答案 0 :(得分:0)
我修改你的代码并将你的计时器包装成一个线程。我减少了计时器和计数,使其更容易测试。我确信有一个更好的方法来编码它,但这个解决方案似乎有效。你可能需要根据需要调整线程睡眠。
您可以通过播放条件
来调整进程何时停止和重新启动if (count > TOTAL || _processStart.AddSeconds(1) < DateTime.Now) )
在函数aTimer_Elapsed中。
如果流程运行超过1秒或计数已到达,则重新启动流程。
class Program
{
private static DateTime _processStart;
static int count = 1;
const int TOTAL = 15;
private static Timer aTimer;
private static Thread _process;
static void Main(string[] args)
{
_process = new Thread(DoProcess);
_process.Start();
Console.WriteLine("Press Enter To Exit The Program\n");
Console.ReadLine();
ProcessExit();
}
static void DoProcess()
{
_processStart = DateTime.Now;
ReadCountFromFile();
if (count < TOTAL)
{
Console.WriteLine("******START TIMER******");
aTimer = new Timer();
aTimer.Elapsed += aTimer_Elapsed;
aTimer.Interval = 500;
aTimer.Enabled = true;
while (aTimer.Enabled)
{
Thread.Sleep(1000);
}
Console.WriteLine("******END TIMER******");
ProcessExit();
DoProcess();
}
}
private static void ReadCountFromFile()
{
try
{
if (File.Exists(".\\mynumber.dat"))
{
using (var file = File.Open(".\\mynumber.dat", FileMode.Open))
{
byte[] bytes = new byte[4];
file.Read(bytes, 0, 4);
count = BitConverter.ToInt32(bytes, 0);
Console.WriteLine("Total count left is = {0} / Limit = {1} / Count = {2}", TOTAL - count, TOTAL, count);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Problem reading file.");
}
}
static void ProcessExit()
{
using (var file = File.Open(".\\mynumber.dat", FileMode.OpenOrCreate))
{
var buffer = BitConverter.GetBytes(count);
file.Write(buffer, 0, buffer.Length);
}
}
private static void aTimer_Elapsed(object source, ElapsedEventArgs e)
{
//Console.WriteLine("Name is Yap {0}", e.SignalTime);
if (count < TOTAL)
{
count += 1;
Console.WriteLine("Count is {0}", count);
}
if (count > TOTAL || _processStart.AddSeconds(1) < DateTime.Now)
{
aTimer.Enabled = false;
Console.WriteLine("Timer is off at {0} count is {1}", e.SignalTime.TimeOfDay.ToString(),count);
}
}
}