Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Thread Abort Exception while using Response.Redirect()

Tags:

I wrote the following piece of code in a page which is under Update Panel.

protected void myGrid_RowCommand(object sender, GridViewCommandEventArgs e) {     try     {         if (e.CommandName =="EditNames")         {             long lSelectedName = Convert.ToInt64(e.CommandArgument);             Session["SelectedItem"] = lSelectedName;             Response.Redirect("EditItem.aspx");         }         else if (e.CommandName =="DeleteNames")         {             long lSelectedName = Convert.ToInt64(e.CommandArgument);             ValidName.DeleteItem(lSelectedName);              ScriptManager.RegisterStartupScript(this, GetType(), "Key", "alert('Name deleted sucessfully')", true);         }     }     catch (System.Threading.ThreadAbortException)     {      }     catch (Exception ex)     {         Error handling code...     } } 

Here, I am getting a Thread Abort Exception while redirecting. However, I resolved it by using an error handler System.Threading.ThreadAbortException.

But I am unsure why that error came while redirecting. Even though I solved this problem, I would like to know is there any mistake in the way I am coding or is there any way to stop the error firing at all.

Give your inputs...

Note that the page is under AJAX UPDATE PANEL.

like image 853
Sai Avinash Avatar asked Aug 27 '13 12:08

Sai Avinash


People also ask

What causes thread Abort exception?

When a call is made to the Abort method to destroy a thread, the common language runtime throws a ThreadAbortException on . NET Framework. ThreadAbortException is a special exception that can be caught, but it will automatically be raised again at the end of the catch block.

How to avoid thread Abort exception in c#?

ThreadAbortException is a special exception that can be caught by application code, but is re-thrown at the end of the catch block unless ResetAbort is called. ResetAbort cancels the request to abort, and prevents the ThreadAbortException from terminating the thread.

Does response redirect stop execution?

When you use Response. Redirect("Default. aspx",true ) which is by default true then the execution of current page is terminated and code written after the Response. Redirect is not executed instead of executing code written after the Response.


2 Answers

Please read this article - http://blogs.msdn.com/b/tmarq/archive/2009/06/25/correct-use-of-system-web-httpresponse-redirect.aspx

Instead of ending the request, it is a good practice to bypass the request execution pipeline by calling the Context.ApplicationInstance.CompleteRequest().

So your code would look something like this:

Response.Redirect("TargetPage", false);        //write redirect Context.ApplicationInstance.CompleteRequest(); // end response 
like image 111
Moiz Tankiwala Avatar answered Sep 26 '22 09:09

Moiz Tankiwala


Even though, i solved this problem , i would like to know is there any mistake in the way i am coding

No mistake, you've done well.

This error is expected. It's thrown because the server thread is in fact aborted when redirecting. From the MSDN documentation:

If you specify true for the endResponse parameter, this method calls the End method for the original request, which throws a ThreadAbortException exception when it completes.

and the documentation for the overload you're using:

Redirect calls End which throws a ThreadAbortException exception upon completion.

like image 37
Mike Perrenoud Avatar answered Sep 24 '22 09:09

Mike Perrenoud