2017-03-08 10 views
0

WindowsサービスをC#言語で構築し始めました。そして私はそれの中にタイマー機能を実装したいと思います。しかし何らかの理由で、DoItタイマーイベントハンドラがデバッグ中に起動されず、例外も発生しません。私は、デバッグ - >新しいインスタンスを開始を使用してWindowsサービスをデバッグしようとしています。C#Windowsサービス - タイマーがデバッグで動作していません

行TraceLog.WriteTrace( "Router Service Started");ヒットして実行されます。

public partial class EntryPoint : ServiceBase 
    { 
     private const int TIMER_INTERVAL = 10000; 
     private System.Timers.Timer mvTimer = new System.Timers.Timer(); 

     [MTAThread()] 
     [System.Diagnostics.DebuggerNonUserCode()] 
     public static void Main() 
     { 
      #if DEBUG 
         EntryPoint service = new EntryPoint(); 
         service.Start(); 
      #else 
         ServiceBase[] ServicesToRun; 
         ServicesToRun = new ServiceBase[] 
         { 
          new EntryPoint() 
         }; 
         ServiceBase.Run(ServicesToRun); 
      #endif 
     } 
     public EntryPoint() 
     { 
      AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); 
      InitializeComponent(); 
     } 

     private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) 
     { 
      Exception ex = (Exception)e.ExceptionObject; 
      if (ex != null) 
      { 
       Console.WriteLine(ex); 
      } 
     } 


     public void Start() 
     { 
      OnStart(null); 
     } 

     protected override void OnStart(string[] args) 
     { 
      TraceLog.SetTrace(ConfigurationManager.AppSettings["RouterTraceLog"]); 
      TraceLog.WriteTrace("Router Service Started"); 

      mvTimer = new Timer(); 
      mvTimer.Elapsed += new ElapsedEventHandler(DoIt); 
      try 
      { 
       mvTimer.Interval = TIMER_INTERVAL; 
       mvTimer.Enabled = true;     
      } 
      catch (Exception ex) 
      { 
       mvTimer.Interval = TIMER_INTERVAL; 
       TraceLog.WriteTrace(ex.Message); 
      } 
     } 

     private void DoIt(object sender, ElapsedEventArgs e) 
     { 
      TraceLog.WriteTrace("Inside DoIt :: " + DateTime.UtcNow.ToString()); 
     } 

     protected override void OnStop() 
     { 
      mvTimer.Enabled = false; 
      TraceLog.WriteTrace("Router Service stopping"); 
     } 

enter image description here

示唆してください。私は非常に小さいものを紛失しており、それを釘付けにすることはできません。

答えて

1

デバッグモードでは、アプリケーションはOnStartの終了後に終了します。アプリケーションの終了を止めるものは何もありません。

アプリケーションが終了しないようにするには、Console.ReadLine();を追加します。

#if DEBUG 
     EntryPoint service = new EntryPoint(); 
     service.Start(); 
     Console.ReadLine(); 
#else 

あなたはどのように上MSDNで詳細を読むことができます:私が試したデバッグのWindowsサービスアプリケーション

+0

が、ReadLineメソッドを()を使用して、中には差は認められません。まだ出口をやっています。 – Karan

+0

はコンソールアプリケーションに設定された出力タイプですか? –

+0

はい、出力タイプをコンソールアプリケーションに変更した後はうまくいきました:-)ありがとうトントン:-) – Karan

関連する問題