【问题标题】:Thread was being aborted when we use当我们使用时线程被中止
【发布时间】:2013-01-16 09:48:18
【问题描述】:

我收到以下异常:

System.Threading.ThreadAbortException:线程被中止。
在 System.Threading.Thread.AbortInternal() 在 System.Threading.Thread.Abort(对象状态信息)在 System.Web.HttpResponse.End() 在 System.Web.HttpResponse.Redirect(String url, Boolean endResponse)
在 System.Web.HttpResponse.Redirect(String url) 在 Taxi_selection.lnkbtnconfirm_Click(Object sender, EventArgs e)

我发现解决方案是使用:

Response.Redirect("home.aspx",false);

但是这个错误又发生了。

对此有什么好的解决方案?

我的代码 sn-ps:

try
{
    Decimal Amount = 0;
    Int64 CabId = 0;
    String CabName = "";
    String CarImage = "";

    foreach (DataListItem gr in dtlstcars.Items)
    {
        RadioButton objcheck = (RadioButton)gr.FindControl("rdbtncarchecked");
        if (objcheck.Checked == true)
        {
            Literal ltrid = new Literal();
            ltrid = (Literal)gr.FindControl("ltrid");

            Label lbtaxiname = (Label)gr.FindControl("lbtaxiname");
            Label lbonewaycarprice = (Label)gr.FindControl("lbonewaycarprice");
            Label lbtwowaycarprice = (Label)gr.FindControl("lbtwowaycarprice");
            Image imgcar = (Image)gr.FindControl("imgcar");

            if (ltrid != null && lbtaxiname != null && imgcar != null && lbonewaycarprice != null && lbtwowaycarprice != null)
            {
                if (lbrootype.Text == "One")
                {
                    Amount = Convert.ToDecimal(lbonewaycarprice.Text);
                }
                else
                {
                    Amount = Convert.ToDecimal(lbtwowaycarprice.Text);
                }
            }
            CabId = Convert.ToInt64(ltrid.Text);
            CabName = lbtaxiname.Text;
            CarImage = imgcar.ImageUrl;
        }
   }
   if (lbroottype.Text != String.Empty && lbrouteid.Text != String.Empty && lbfrom.Text != String.Empty && lbpickupdate.Text != String.Empty && lbto.Text != String.Empty && lbpickupdate.Text != String.Empty && lbpickuptime.Text != String.Empty)
   { 
        Session.Add("BookingDetail", BookingDetail(lbroottype.Text, Convert.ToInt64(lbrouteid.Text), lbfrom.Text, lbto.Text, Convert.ToDateTime(lbpickupdate.Text), lbpickuptime.Text, Convert.ToDateTime(lbreturndate.Text), String.Empty, CabId, CabName, CarImage, Amount, txtPickupaddress.Text, txtDropaddress.Text, txtlandmark.Text, txtname.Text, ddmobilestdcode.SelectedValue, txtmobileno.Text, ddalternatestdcode.SelectedValue, txtalternateno.Text, txtemail.Text, lbdays.Text));//3
       Session.Remove("cart");
       Session.Remove("editcart");
       Response.Redirect("confirm");
   }
   else
   {
       Response.Redirect("home");
   }
}
catch (Exception ext)
{
    String msg = ext.Message;
    da.InsertRecordWithQuery("insert error_tbl values('" + msg + "')");
}

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    当您不让页面的其余部分继续运行时,这就是重定向的工作方式。它停止线程并抛出该中止异常。你可以简单地忽略它:

    try
    {
        Response.Redirect("newpage.aspx", true);
    }
    catch (System.Threading.ThreadAbortException)
    {
        // ignore it
    }
    catch (Exception x)
    {
    
    }
    

    注意

    如果您在不停止其余处理的情况下调用重定向,则可以使用 NoRedirect 之类的插件停止重定向过程的黑客可以看到您页面的其余部分。!

    为了证明我的观点,我提出一个问题:Redirect to a page with endResponse to true VS CompleteRequest and security thread

    【讨论】:

    • 不要这样做。从技术上讲,您可以在此处捕获ThreadAbortException,但它会继续冒泡。调用Response.End()generally felt as being bad,因为它很苛刻。在大多数情况下,您可以改用HttpApplication.CompleteRequest()
    • @Sumo 如果你不停止页面的编译,那么任何黑客都可以看到渲染的其余部分。是的,你抓住并忽略了线程中止,我不明白问题是什么,这就是你所做的 - 你中止线程以移动到其他页面。
    • 提高ThreadAbortExceptions 从来都不是好事。它们是一种特殊情况,将在 catch 块的末尾再次引发。 finally 块将在它尝试销毁线程之前运行,并且不能保证它会这样做。有一些替代方法可以防止完成渲染。我更新了对演示它的链接的答案。
    • 将您的观点与覆盖 Render 和 RaisePostBackEvent 进行比较,正如我在对避免渲染任何内容的解决方案的回答中所说的那样。这比中止线程要好。我也在你的新问题中给出了答案。
    【解决方案2】:

    Response.Redirect 没有将endResponse 参数指定为false(默认为true)将在内部调用Response.End(),因此将触发ThreadAbortException 以停止执行。

    这里推荐两件事之一:

    1. 如果您需要结束响应,请不要在 try/catch 中执行。这将导致重定向失败。

    2. 如果您不需要结束响应,请改为调用:

      Response.Redirect(url, false);

    在 try/catch 内:

    try {
        // do something that can throw an exception
        Response.Redirect(url, false);
        HttpContext.Current.ApplicationInstance.CompleteRequest();
    } catch (SomeSpecificException ex) {
        // Do something with the caught exception
    }
    

    为避免回发处理和 HTML 呈现,您需要做更多工作:

    http://web.archive.org/web/20101224113858/http://www.c6software.com/codesolutions/dotnet/threadabortexception.aspx

    【讨论】:

    【解决方案3】:

    http://support.microsoft.com/kb/312629

    正如您在此处看到的,问题是您正尝试在 try/catch 块中使用 response.redirect。它抛出了一个异常。

    您将呼叫更改为Response.Redirect(url, false) 的解决方案应该有效。您需要确保在每次 Response.Redirect 调用时都执行此操作。

    还请注意,这将继续执行,因此您必须处理它(防止它以其他方式继续执行)。

    【讨论】:

      猜你喜欢
      • 2010-09-26
      • 1970-01-01
      • 2013-02-27
      • 2012-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-24
      • 1970-01-01
      相关资源
      最近更新 更多