【问题标题】:Handling Unhandled errors that happen in a secondary threaded process处理在辅助线程进程中发生的未处理错误
【发布时间】:2009-03-09 15:21:13
【问题描述】:

我有一个 asp.net 2.0 Web 应用程序,它调用一个长时间运行的存储过程(大约 3 分钟)。 sp 实际上在后端执行了许多任务。

我的页面使用线程和 ajax(更新面板)和计时器控件向用户显示进度条。这一切都很好,除非 sp 中出现错误,只是冻结屏幕。

我尝试使用我在codeProject 上找到的 SafeThread 类,它包装了线程进程并创建了一个可以在发生异常时处理的事件。

在事件处理程序中,我只想将用户重定向到错误页面并显示错误。测试时我可以中断事件处理程序。然而,调用 Server.Transfer 或 Response.Redirect 完全没有效果。

我不确定为什么会发生这种情况。我将在下面发布我的代码。任何想法或替代建议表示赞赏。

    protected void btnSave_OnClick(object sender, EventArgs e)
    {
        DAC dac = new DAC();
        Session["RerunStatus"] = 0;
        SafeThread thrd = new SafeThread(new ParameterizedThreadStart(dac.RerunTest));
        thrd.IsBackground = true;

        //tell the SafeThread to report 
        //ThreadAbort exceptions
        thrd.ShouldReportThreadAbort = true;
        //attach a ThreadException handler for 
        //reporting SafeThread exceptions
        thrd.ThreadException += new
        ThreadThrewExceptionHandler(thrd_ThreadException);

        thrd.Start(ddlRundate.SelectedItem.Text);
        Session["RerunThread"] = thrd;
        btnSave.Enabled = false;
        Timer1.Enabled = true;
    }

    void thrd_ThreadException(SafeThread thrd, Exception ex)
    {
        //thrd.Abort();
        Timer1.Enabled = false;
        //Response.Redirect("ErrorPage.aspx");
        Server.Transfer("ErrorPage.aspx?ErrorMsg=" + ex.Message);
        //thrd.Abort();
    }

【问题讨论】:

  • 为了更容易理解(和回答)我试着把你的文字分成几段。请检查我是否没有过多地改变含义。如果是,请回滚。
  • 我想这是asp.net?

标签: c# asp.net multithreading error-handling response.redirect


【解决方案1】:

您只能在处理请求时使用 Server.Transfer()/Response.Redirect() - 调用 thrd_ThreadException() 时并非如此。

尝试在您的 thrd_ThreadException 中设置一个标志并在 Timer1 Tick 事件中执行重定向。

【讨论】:

    猜你喜欢
    • 2014-10-28
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    相关资源
    最近更新 更多