未处理的异常 C#

4

我有这段代码,我使用计时器调用我的RunTest()函数。如果我不使用计时器,所有未处理的异常都会被捕获,但是使用计时器代码时它们没有被捕获。我犯了什么错误?

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
你没有使用try catch块,所以显然异常没有被捕获。 - ThePerplexedOne
无论如何,异常是在计时器滴答时抛出的,这是在单独的线程上运行的。因此,您的AppDomain.CurrentDomain是多余的。正确处理异常。 - ThePerplexedOne
就像我说的那样,使用 try catch - ThePerplexedOne
那并没有帮助!@ThePerplexedOne - Sidharth Mittal
1
System.Timers.Timer是一个非常糟糕的类。它会吞噬在Elapsed事件处理程序中抛出的任何异常。不要使用它,而应该使用System.Threading.Timer。 - Hans Passant
显示剩余5条评论
1个回答

2

有记录表明:

计时器组件会捕获并抑制所有 Elapsed 事件处理程序抛出的异常。

您需要在 OnTimedEvent 方法中捕获异常,然后通过另一个线程将其传播回去,例如通过 ThreadPool.QueueUserWorkItem() 中抛出它,以便 CurrentDomain_UnhandledException 可以捕获它。

或者创建一个处理程序:

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

将其传递给 ElapsedEventHandler
aTimer.Elapsed += (sender, e) => { OnTimedEvent(sender, e, TimerExceptionHandler); };

当抛出异常时调用它:

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

谢谢!问题已经得到解决,使用了其他人建议的 System.Threading.Timer。 - Sidharth Mittal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接