2012-03-02 77 views
0

如何在一天结束时自动重置计时器,以及如何显示最后一次执行的时间和日期? 该程序是 -如何在一天结束时自动重置计时器?

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

你是什么意思在一天结束时重置计时器?停止你的应用程序或执行“进程退出”并再次重新启动该进程 – Guillaume 2012-03-02 13:28:19

+0

执行进程退出并重新启动该进程。 – 3692 2012-03-04 07:06:46

+0

@Guillaume如果我必须停止我的应用程序?我也想显示计数,时间和日期每次我已经完成该过程后,并重新启动它,下次我运行它.. – 3692 2012-03-06 04:46:46

回答

0

我修改你的代码并包装你的计时器到一个线程。我减少了定时器,并计数以便测试。我相信有更好的方法来编码它,但这个解决方案似乎工作。您可能需要根据您的需要调整线程睡眠。

那么您可以调整过程中,应停止并通过与aTimer_Elapsed功能的条件

if (count > TOTAL || _processStart.AddSeconds(1) < DateTime.Now)) 

打重启。

当进程运行超过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); 
     } 
    } 
}