ASP.net Application_Start计时器中捕获异常



给定Global.asax中的以下代码,抛出的第一个异常被正确捕获,但计时器异常没有。

我需要更改什么才能捕获计时器中的任何异常?

protected void Application_Start(object sender, EventArgs e)
{
    // THIS WORKS
    try
    {
        throw new Exception("Test!");
    }
    catch (Exception ex)
    {
        Code.Helpers.Error.Functions.RecordError(ex);
    }
    // THIS DOESN'T WORK
    try
    {
        var myTimer = new Timer(
            Code.Helpers.MyTimer.Process,
            null,
            new TimeSpan(0, 0, 0, 0),
            Settings.ProcessMyTimerEvery);
    }
    catch (Exception ex)
    {
        Code.Helpers.Error.Functions.RecordError(ex);
    }
}

来自System.Threading.Timer文档(强调我的):

提供以指定间隔在线程池线程上执行方法的机制。

也值得一读:

回调指定的方法应该是可重入的,因为它是在 ThreadPool 线程上调用的。如果计时器间隔小于执行该方法所需的时间,或者所有线程池线程都在使用中并且该方法多次排队,则可以在两个线程池线程上同时执行该方法。

这意味着您传递给计时器的委托不会在计时器所在的同一线程上调用。若要在计时器事件中捕获异常,需要将 try/catch 放入其中。例如:

var myTimer = new Timer(
    TimerEvent, //<-- A delegate to the TimerEvent method
    null, 
    new TimeSpan(0, 0, 0, 0), 
    new TimeSpan(0, 0, 0, 5));

还有您的计时器代码:

private void TimerEvent(object x)
{
    try
    {
        throw new Exception("Exception in timer event");
    }
    catch (Exception ex)
    {
        Code.Helpers.Error.Functions.RecordError(ex);
    }
}

您应该将 try/catch 块放在计时器回调中(在您的情况下Code.Helpers.MyTimer.Process)。

最新更新