2016-12-01 3 views
4

Ich habe dieses Stück Code, ich verwende einen Timer, um meine RunTest() - Funktion aufzurufen. Wenn ich keinen Timer verwende, werden alle unbehandelten Ausnahmen in RunTest() abgefangen, aber mit diesem Timer-Code werden sie nicht abgefangen. Welchen Fehler mache ich?unhandledexception C#

private static TelemetryClient telemetryClient = new TelemetryClient(); 

private static System.Timers.Timer aTimer; 

    static void Main(string[] args) 
    { 

     // Register Unhandled Exception Handler 
     AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; 
     // Set the Instrumentation Key to track the WebJob Exception 
     TelemetryConfiguration.Active.InstrumentationKey = SecureConfigReader.GetSetting("ApplicationInsightsKey"); 


     Configure(); 

     if (args.Length > 0) 
     { 
      // Create a timer with a ten second interval. 
      aTimer = new System.Timers.Timer(10000); 
      // Hook up the Elapsed event for the timer. 
      aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); 
      double Scheduled_Interval = Convert.ToDouble(args[0]); 
      // Set the Interval to 300 seconds. 
      aTimer.Interval = TimeSpan.FromSeconds(Scheduled_Interval).TotalMilliseconds; 
      aTimer.Enabled = true; 

      Console.WriteLine("Press the Enter key to exit the program."); 
      Console.ReadLine(); 
     } 
    } 


    private static void OnTimedEvent(object source, ElapsedEventArgs e) 
    { 
     Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime); 
     RunTests(); 

    } 
    private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) 
    { 
     ExceptionTelemetry excTelemetry = new ExceptionTelemetry((Exception)e.ExceptionObject); 
     excTelemetry.SeverityLevel = SeverityLevel.Critical; 
     excTelemetry.HandledAt = ExceptionHandledAt.Unhandled; 
     telemetryClient.InstrumentationKey = SecureConfigReader.GetSetting("ApplicationInsightsKey"); 
     telemetryClient.TrackException(excTelemetry); 
     //make sure all telemetry is sent before closing the app 
     telemetryClient.Flush(); 
    } 

    /* 
    * 1. Find all tests 
    * 2. Run all tests, collecting all results 
    * 3. Process alerts if any 
    * 4. Save results 
    */ 
    private static void RunTests() 
    { 

     List<SyntheticTransactionTableEntity> results = new List<SyntheticTransactionTableEntity>(); 
     List<ISyntheticTransaction> tests = new List<ISyntheticTransaction>(); 
    } 
+1

Sie nicht einen Versuch catch-Block haben, so offensichtlich sind Ausnahmen nicht erwischt zu werden. – ThePerplexedOne

+0

ich denke, die Syntax ist 'AppDomain.CurrentDomain.UnhandledException + = neue UnhandledExceptionEventHandler (CurrentDomain_UnhandledException)' – bansi

+0

@ThePerplexedOne er verwendet [AppDomain.CurrentDomain.UnhandledException] (https://msdn.microsoft.com/en-us/library/ system.appdomain.unhandledexception (v = vs.110) .aspx) – bansi

Antwort

2

Sein dokumentiert, dass:

Die Timer-Komponente Fänge und unterdrückt alle von Event-Handler für das Elapsed-Ereignis ausgelöste Ausnahmen.

Sie müssen innerhalb der OnTimedEvent Methode fangen und dann diese Ausnahme propagieren wieder aus, zB indem sie auf einem anderen Thread zu werfen, wenn Sie CurrentDomain_UnhandledException wollen, es fangen (throw in ThreadPool.QueueUserWorkItem())

Alternativ einen Handler erstellen:

private static void TimerExceptionHandler(Exception ex) 
{ 
    Console.WriteLine(ex.ToString()); 
    ... 
} 

Pass an die ElapsedEventHandler:

aTimer.Elapsed += (sender, e) => { OnTimedEvent(sender, e, TimerExceptionHandler); }; 

Nennen Sie es, wenn eine Ausnahme ausgelöst wird:

private static void OnTimedEvent(object source, ElapsedEventArgs e, Action<Exception> timerExceptionHandler) 
    try 
    { 
     RunTests(); 
    } 
    catch (Exception ex) 
    { 
     timerExceptionHandler(ex); 
    } 
+0

Danke! Das Problem wurde mit System.Threading.Timer behoben, wie von jemandem vorgeschlagen. –