2016-02-28 63 views
0

我寫了每分鐘重複執行一些任務的應用程序。問題是,有時(內部計算(因爲CPU很忙或者因爲任何原因)需要超過50秒,那麼我必須關閉應用程序。「獨立應用程序的應用程序監視器將再次打開它」,所以這不是問題。我實現了看門狗,它可以告訴我時間是否完成,然後關閉我的應用程序 而我的問題是,如果我正確地做到了這一點,我測試這個解決方案一段時間,它看起來像工作正常。主程序C# - 計算需要更多時間時關閉控制檯應用程序

using System; 
using System.Collections.Generic; 
using System.Diagnostics; 
using System.Linq; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 

namespace AppTimer 
{ 
    class Program 
    { 
     public static appScheduler ap = new appScheduler(); 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Press ESC to stop"); 
      while (ap.getWatchDogSeconds() < 55) 
      { 
       if (Console.KeyAvailable) 
       { 
        if (Console.ReadKey(true).Key == ConsoleKey.Escape) 
        { 
         break; 
        } 
       } 
       Thread.Sleep(100); 
      } 
      Console.WriteLine("Watchdog Fired ....sw finished !!"); 
      Thread.Sleep(2000); 
     } 

     /// <summary> 
     /// Emulation of long calculation process 
     /// </summary> 
     public static void doJob() 
     { 
      Console.Clear(); 
      Thread.Sleep(5000); 
      Console.WriteLine("Busy..."); 
      Console.WriteLine("WatchDog Seconds : " + ap.getWatchDogSeconds()); 
      ap.setWatchDogSeconds(40); 
      Console.WriteLine("Waiting longer than normal..."); 
      Console.WriteLine("WatchDog Seconds : " + ap.getWatchDogSeconds()); 
      Thread.Sleep(5000);   
      Console.WriteLine("Maybe sth is down !! or too busy"); 
      Console.WriteLine("WatchDog Seconds : " + ap.getWatchDogSeconds()); 
      while (true) 
      { 
       Thread.Sleep(200); 
      } 
     } 
    } 
} 

下面appScheduler類:

using System; 
using System.Collections.Generic; 
using System.Diagnostics; 
using System.Linq; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 

namespace AppTimer 
{ 
    public class appScheduler 
    { 
     static readonly object _lockerWD = new object(); 
     static int minutes = 0; 
     static int seconds = 50; 
     static int days = 0; 
     static int hours = 0; 
     private static Task calculationsTask; 
     private int watchDogSeconds = 0; 
     private int maxSecondsForWatchdog = 50; 

     public appScheduler() 
     { 
      startTimer(); 
      startWatchdogTimer(); 
     } 

     private void ExecuteSeconds() 
     { 
      if (seconds == 0) 
      { 
       executeRepeatdTask(); 
      } 
      calculateTime(); 
      showTimerScreen(); 
     } 

     private void executeRepeatdTask() 
     { 
      calculationsTask = Task.Factory.StartNew(() => 
       Program.doJob()); 
     } 

     public void startTimer() 
     { 
      var timer = new System.Threading.Timer((e) => 
      { 
       ExecuteSeconds(); 
      }, null, 0, 1000); 
     } 

     public void startWatchdogTimer() 
     { 
      var timer = new System.Threading.Timer((e) => 
      { 
       ExecuteSecondsWatchDog(); 
      }, null, 0, 1000); 
     } 

     private void ExecuteSecondsWatchDog() 
     { 
      Trace.WriteLine("Current amount of WD Seconds : " + watchDogSeconds); 
      increaseWatchDogSeconds(); 
     } 

     private void showTimerScreen() 
     { 
      if ((calculationsTask == null) || (calculationsTask.IsCompleted == true)) 
      { 
       //making watchdog zero if time is running and we are showing it!! 
       Console.Clear(); 
       Console.WriteLine("This is software v1.2"); 
       Console.WriteLine(String.Format("Current execution time : {0} days : {1} hours : {2} : minutes : {3} seconds", 
         days, hours, minutes, seconds)); 
       Console.WriteLine("WatchDog Seconds : " + watchDogSeconds); 
       setWatchDogSeconds(0); 
      } 
     } 

     private void calculateTime() 
     { 
      seconds++; 
      if (seconds > 59) 
      { 
       seconds = 0; 
       minutes++; 
       if (minutes > 59) 
       { 
        minutes = 0; 
        hours++; 
        if (hours > 23) 
        { 
         hours = 0; 
         days++; 
        } 
       } 
      } 
     } 

     public int getMinutes() 
     { 
      return minutes; 
     } 
     public int getWatchDogSeconds() 
     { 
      return watchDogSeconds; 
     } 
     public void setWatchDogSeconds(int seconds) 
     { 
      Monitor.Enter(_lockerWD); 
      watchDogSeconds = seconds; 
      Monitor.Exit(_lockerWD); 
     } 

     public void increaseWatchDogSeconds() 
     { 
      var seconds = getWatchDogSeconds(); 
      setWatchDogSeconds(seconds += 1); 
      if (seconds > maxSecondsForWatchdog) 
      { 
       Trace.WriteLine(string.Format("More than {0} seconds!", maxSecondsForWatchdog)); ; 
       Environment.Exit(0); 
      } 
     } 
    } 
} 
+0

那麼問題是什麼?你說它行得通... ...? –

+1

如果您想每隔1分鐘運行一次應用程序,則可能需要查看「任務計劃程序」。您可以將應用程序配置爲關閉並每分鐘重新開始一次。 –

+0

喬納森卡羅爾 - 問題是如果除了工作好,後面的想法是好的。 inquisitive_mind - 嗯,這可能是一個好主意,而不是內部複雜的邏輯。我應該殺了應用程序,如果它會工作太長,並再次啓動它。 –

回答

-1

我每天都在做大量的工作流類型任務。我試圖按照過去的方式開展工作。我使用Thread.Sleep的唯一時間是在測試環境中模擬一些長時間運行的進程。我會考慮使用SetTimer。在循環中使用Thread.Sleep也不會給你準確的結果。這就是說,保持這種運行方式將會非常棘手。

如果你有一個SQL服務器,並且可以在SSIS包中設置調用,最終定期開始工作是一件輕而易舉的事情。特別是如果你正試圖在一段時間內停止運行的Web應用程序中執行此操作。好運

相關問題