System.Threading.ThreadAbortException: Aborting thread.

Source: Internet
Author: User
Tags httpcontext mscorlib try catch

The first accidental "mscorlib.dll" type of exception in System.Threading.ThreadAbortException
An exception of type "System.Threading.ThreadAbortException" occurs in mscorlib.dll, but is not processed in user code
It does not affect the normal operation of the program. So on-line search, found that the relevant information is not many. Later found Microsoft's official explanation, fix.
--------------------------------------------------------------------------------------------------------------

Symptoms If you use Response.EndResponse.RedirectOr Server.Transfermethod is present, the ThreadAbortExceptionAbnormal. can be used Try-catchStatement to catch this exception. Reason Response.Endmethod to stop the execution of a page and transform the execution into an application's event pipeline. Application_EndRequestEvent. Response.EndThe following line of code will not execute.
This issue occurs in Response.RedirectAnd Server.Transfermethod, this is because both of these methods are internally called Response.End。 Solution to resolve this issue, use one of the following methods:
    • For Response.End, call the applicationinstance.completerequest method without calling Response.Endto skip the code execution for the Application_EndRequest event.
    • For Response.Redirect, use the overloaded response.redirect (String URL, bool endresponse)for endresponse Parameter it passes false to cancel an internal call to Response.End . For example:
        Response.Redirect ("Nextpage.aspx", false);
      If you use this workaround, the code behind theResponse.Redirect will be executed.
    • For Server.Transfer, use the Server.Execute method instead.
State This behavior is by design.

Another problem arises:

This problem also occurs with Response.Redirect () and Server.Transfer () when using Response.End () to throw "thread aborted" exceptions in Try catch.

such as: (


Try
{
if (DoSomething ())
{
Response.End ();
HttpContext.Current.ApplicationInstance.CompleteRequest ();
}
Dootherthing is not written in else just to illustrate the problem.
Dootherthing ();
}
catch (System.Threading.ThreadAbortException ex)
{
Wirtelog (ex);
}
catch (Exception ex)
{
Wirtelog (ex);
}

)

If no catch (System.Threading.ThreadAbortException ex) is used, the "thread aborted" exception is thrown.

If no catch (System.Threading.ThreadAbortException ex) is used, the HttpContext.Current.ApplicationInstance.CompleteRequest Instead of Response.End (), the subsequent dootherthing () will continue to execute.

Choose specific practices based on actual needs.

Final Solution:
(1), in the page onload or oninit can use HttpContext.Current.ApplicationInstance.CompleteRequest () +return to replace the Response.End;
(2), with Response.Redirect (Strurl,false) +return to replace Response.Redirect (strURL)

(3), Server.Execute can not replace the Server.Transfer method, Server.Execute will be two pages of code all output.
Special return NOTE: The return statement exits the method in which this statement is located, and cannot exit the subclass Method! Do not think in the base class page onload in the detection of a object==null can return this request, sub-class page onload no need to detect this object!=null can replicable!

Do this site need to download software can I do not want to let Thunderbolt and other tools to download the impact speed.

I made a restraining procedure!

But there was an error! Aborting thread

The procedure is generally as follows:

    1. try
    2. {  
    3. Sting host = context. request.urlreferrer.host;  
    4. if (program judgment)   
    5. {     
    6. context. Response.Clear ();  
    7. context. Response.ContentType = "Application/octet-stream";  
    8. context. Response.WriteFile (file. FullName);  
    9. context. Response.End ();  
    10. }  
    11. }  
    12. catch (Exception e)    ;
    13. {  
    14. HttpContext.Current.Response.Redirect ("/default.aspx?key=" + e.message);  & nbsp
    15. context. Response.End ();  
    1. Try
    2. {
    3. Sting host = context. Request.UrlReferrer.Host;
    4. if (program judgment)
    5. {
    6. Context. Response.Clear ();
    7. Context. Response.ContentType = "Application/octet-stream";
    8. Context. Response.WriteFile (file. FullName);
    9. Context. Response.End ();
    10. }
    11. }
    12. catch (Exception e)
    13. {
    14. HttpContext.Current.Response.Redirect ("/default.aspx?key=" + e.message);
    15. Context. Response.End ();
    16. }

There must be something wrong with just one execution Try. e.message= aborting Thread

Studied the context as if it were a try execution. Response.End (); Is wrong

Can't put the context. Response.End (); put it in a Try

Need to write:

    1. Try
    2. {
    3. Sting host = context. Request.UrlReferrer.Host;
    4. }
    5. catch (Exception e)
    6. {
    7. HttpContext.Current.Response.Redirect ("/default.aspx?key=" + e.message);
    8. Context. Response.End ();
    9. }
    10. if (program judgment)
    11. {
    12. Context. Response.Clear ();
    13. Context. Response.ContentType = "Application/octet-stream";
    14. Context. Response.WriteFile (file. FullName);
    15. Context. Response.End ();
    16. }

System.Threading.ThreadAbortException: Aborting thread.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.