Skip to main content

Command Palette

Search for a command to run...

Thread was being aborted

Updated
2 min read
Thread was being aborted

System.Threading.ThreadAbortException: Thread was being aborted. at System.Threading.Thread.AbortInternal()

To avoid the ThreadAbortException error when using Response.Redirect, setting the second parameter to false is generally sufficient. However, in this case, the Response.Redirect process does not fully terminate, and subsequent code execution continues. Therefore, adding Context.ApplicationInstance.CompleteRequest() is a more robust approach.


Why?

  • Response.Redirect(url, true) (Default Behavior):

    • By default, the second parameter is true, causing Response.Redirect to throw a ThreadAbortException and preventing the remaining code from executing.

    • This can lead to errors or unnecessary processing overhead.

  • Response.Redirect(url, false):

    • When the second parameter is set to false, no ThreadAbortException is thrown, and the code continues to execute. However, this might cause undesired behavior because the process does not terminate cleanly.
  • Context.ApplicationInstance.CompleteRequest():

    • This method bypasses the remaining pipeline and immediately sends the response to the client.

    • It ensures a safer and more controlled termination of the process.


Correct Usage Example

To handle this properly, you can use the following structure:

Response.Redirect("your-url.aspx", false);
Context.ApplicationInstance.CompleteRequest();

Key Notes

  • Prevent Subsequent Code Execution: If you use Response.Redirect(url, false) but forget to call CompleteRequest, the code following the Redirect will continue to execute, potentially causing unintended side effects.

  • Better Performance: Avoiding ThreadAbortException improves your application's performance and prevents unnecessary error logs.


Summary

While Response.Redirect(url, false) alone is usually enough to avoid the ThreadAbortException error, adding Context.ApplicationInstance.CompleteRequest() ensures that the process is terminated properly and prevents any subsequent code execution. Using both together is the recommended approach.