C# 我们使用时线程被中止

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14640253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:32:41  来源:igfitidea点击:

Thread was being aborted when we use

c#asp.net

提问by Kavita

I'm getting the following exception:

我收到以下异常:

System.Threading.ThreadAbortException: Thread was being aborted.
at System.Threading.Thread.AbortInternal() at System.Threading.Thread.Abort(Object stateInfo) at System.Web.HttpResponse.End() at System.Web.HttpResponse.Redirect(String url, Boolean endResponse)
at System.Web.HttpResponse.Redirect(String url) at taxi_selection.lnkbtnconfirm_Click(Object sender, EventArgs e)

System.Threading.ThreadAbortException: 线程被中止。
在 System.Threading.Thread.AbortInternal() 在 System.Threading.Thread.Abort(Object stateInfo) 在 System.Web.HttpResponse.End() 在 System.Web.HttpResponse.Redirect(String url, Boolean endResponse)
在 System. Web.HttpResponse.Redirect(String url) at tax_selection.lnkbtnconfirm_Click(Object sender, EventArgs e)

I found that the solution for this is to use:

我发现解决方案是使用:

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

but again this error is occurring.

但再次发生此错误。

What is a good solution for this?

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

my code snippets :

我的代码片段:

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 + "')");
}

采纳答案by AnthonyM

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

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

as you can see here the problem is that you are attempting to use response.redirect in a try/catch block. It thrown an exception.

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

Your solution of changing the call to be Response.Redirect(url, false)should work. You need to make sure to do it on every Response.Redirect call.

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

Also note that this will continue execution, so you will have to handle that (prevent it from continuing in some other way).

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

回答by Aristos

This is the way the Redirect works when you do not let the rest of the page continue to run. Its stop the thread and throw that abort exception. You can simple ignore it as:

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

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

}

Attention

注意力

If you call the redirect with out stopping the rest of the processing, a hack that can stop the redirect process using a plugin like the NoRedirectcan see your rest of the page .!

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

To prove my point here I make a question about : Redirect to a page with endResponse to true VS CompleteRequest and security thread

为了在这里证明我的观点,我提出了一个问题:重定向到带有 endResponse 的页面到真正的 VS CompleteRequest 和安全线程

回答by Sumo

Response.Redirectwithout specifying the endResponseparameter as false(default is true) will call Response.End()internally and therefore will trigger a ThreadAbortExceptionto stop execution.

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

One of two things are recommended here:

这里推荐两件事之一:

  1. If you need to end the response, do not do it in a try/catch. This will cause the redirect to fail.

  2. If you do not need to end the response, call this instead:

    Response.Redirect(url, false);

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

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

    Response.Redirect(url, false);

Within try/catch:

在 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
}

To avoid postback handling and HTML rendering, you need to do more:

为了避免回发处理和 HTML 呈现,您需要做更多的事情:

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

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