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.End、
Response.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:
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:
- try
- {
- Sting host = context. request.urlreferrer.host;
- if (program judgment)
- {
- context. Response.Clear ();
- context. Response.ContentType = "Application/octet-stream";
- context. Response.WriteFile (file. FullName);
- context. Response.End ();
- }
- }
- catch (Exception e) ;
- {
- HttpContext.Current.Response.Redirect ("/default.aspx?key=" + e.message); & nbsp
- context. Response.End ();
-
- }
- Try
- {
- Sting host = context. Request.UrlReferrer.Host;
- if (program judgment)
- {
- Context. Response.Clear ();
- Context. Response.ContentType = "Application/octet-stream";
- Context. Response.WriteFile (file. FullName);
- Context. Response.End ();
- }
- }
- catch (Exception e)
- {
- HttpContext.Current.Response.Redirect ("/default.aspx?key=" + e.message);
- Context. Response.End ();
- }
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:
- Try
- {
- Sting host = context. Request.UrlReferrer.Host;
- }
- catch (Exception e)
- {
- HttpContext.Current.Response.Redirect ("/default.aspx?key=" + e.message);
- Context. Response.End ();
- }
- if (program judgment)
- {
- Context. Response.Clear ();
- Context. Response.ContentType = "Application/octet-stream";
- Context. Response.WriteFile (file. FullName);
- Context. Response.End ();
- }
System.Threading.ThreadAbortException: Aborting thread.