2012-03-02 18 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

1日の終わりにタイマーをリセットするとどういう意味ですか?アプリケーションを停止するか「プロセス終了」を実行してもう一度プロセスを再開してください – Guillaume

+0

プロセスを終了してもう一度プロセスを再開してください。 – 3692

+0

@Guillaumeアプリケーションを停止する必要がある場合はどうすればよいですか?私はまた、プロセスを完了した後、カウント、時間と日付を毎回表示したいと私はそれを次回起動時に再起動します.. – 3692

答えて

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); 
     } 
    } 
} 
関連する問題