2012-03-06 4 views
1

一度にカウントを取得するにはどうすれば同じページで実行を停止できますか?たとえば、私がそれを止めるまで、今度は線が繰り返されています。私はプロセスを開始するときに残っている数を表示します。しかし、私はリピートラインを停止した後に残りのカウントが必要です。どのように実行を停止し、同じ出力画面で結果が一度停止するのですか?

誰か助けてください。

プログラムがある -

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); 
         Console.WriteLine("Count: {0} of 10000. {1} remaining", count, 10000 - 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

参照フレドリックの答え:http://stackoverflow.com/questions/1119841/net-console-application-exit-event - 別のスレッドで実行されている空の「メッセージポンプ」との完全なプログラム例本質的に彼はメッセージポンプを使用して、コンソールアプリケーションProcesssExitsの前にイベントをトラップします。 「あなたのアプリケーションを正常に終了する方法」が必要であることを示すコメントがあるので、CurrentDomain_ProcessExitを、その結果を印刷した後にInturnがCurrentDomain_ProcessExitを呼び出す別の関数に置き換えることができます。 EricLaw -MSFT- answerも参照してください。 HTH –

+0

完全なソースコードを含む最終的な解決策? – Kiquenet

答えて

0

次のようなコードの最後の3行を変更します。アプリケーションはコンソールアプリケーションでなければなりません。ここ

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"); 
//Modification starts from here 
      AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit); 
      Console.ReadLine(); 
//Modification ends from here 
     } 
関連する問題