【发布时间】:2012-03-02 04:54:13
【问题描述】:
我如何以相反的顺序获得计数?现在,我已经显示了完成的计数。但我也想要剩下的计数。以及如何在下面的程序中每天结束时重置计时器并显示上次执行的时间?有人可以帮我完成整个程序吗? 该程序是 -
namespace Time_Writer
{
class Program
{
static int count = 1;
static double seconds;
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);
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());
}
}
}
}
【问题讨论】:
-
你能解释一下你的程序应该做什么吗?为什么要写入文件?它在读什么?
-
@gideon 我不认为这里发布的整个代码不需要他的问题。
-
@gideon 实际上这个程序是每天重复给定的语句 10k 次。我可以插入邮件并将其发送给不同的收件人,而不是语句。它写入文件是因为我需要记录已发送邮件和剩余邮件的计数。你能帮帮我吗?
-
@Jayanga 那又怎样?我只是发布它,这样人们就可以真正看到程序是关于 n 的,然后得到一个线索..