Loading ...

Getting Thread was being aborted error on Context.Response.End()

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  getting started / general asp.net   » Getting Thread was being aborted error on Context.Response.End()

Getting Thread was being aborted error on Context.Response.End()

Posts under the topic: Getting Thread was being aborted error on Context.Response.End()

Posted: 7/27/2011

Starter 1414  points  Starter
  • Joined on: 12/1/2008
  • Posts: 66

Hello,

Im trying to end a Response but on doing so getting this error Thread was being aborted.

Below is my code.

        try
        {
            string searchText = "";
            if (Request.QueryString["q"] != null)
            {
                searchText = Request.QueryString["q"];
            }

            var users= GetUsers(searchText);
            if (users!= null) Response.Write(users);
            Response.End();
        }
        catch (Exception ex)
        {
           objLogger.WriteLogger(CommonConfiguration.GetChannelId(), ex.Message, 
            "Page_Load", "AutoComplete", "ERR", "");
        }

I'm unable to understand why Im getting this error. If I comment Response.End(). That error doesn't come any more.

Can any help me out. Why im getting this error?

Thanks in advance 

Sumit Arora


Posted: 7/27/2011

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490
  Answered

Hi,

You should note one thing that methods like Response.Redirect or Response.End will throw ThreadAbortException to end the current processing of page. This is a kind of special exception. From the same link: 
ThreadAbortException is a special exception that can be caught, but it will automatically be raised again at the end of the catch block.
You can ignore this exception and if you want not to log this, you can go like this:

        //Response.End() will throw a ThreadAbortException
        try
        {

            Response.End();
        }
        catch (System.Threading.ThreadAbortException ex)
        {
            //You can ignore this 
        }
        catch (Exception ex)
        {
            //Log your other exceptions
        }

 


Posted: 7/27/2011

Professional 8505  points  Professional
  • Joined on: 5/3/2010
  • Posts: 391
  Answered

Hi Sumit,

You can also check my blog post I have written sometime ago:

http://weblogs.asp.net/hajan/archive/2010/09/26/why-not-to-use-httpresponse-close-and-httpresponse-end.aspx

It might be helpful.

Regards,
Hajan 


Page 1 of 1 (3 items)