Home All Groups Group Topic Archive Search About

Exception: Thread was being aborted

Author
16 Dec 2005 9:44 PM
Jeff
I have some code that enables users to download the contents of a DataGrid
as an Excel file. The following code snippet is the relevant part. It works
just fine - meaning specifically that the downloaded Excel file is what I
want.

The problem is that every time this code runs, my "catch all" exception
handler (an HTTP Module) catches the "Thread was being aborted" exception
(from System.Threading.Thread.AbortInternal()).

What can I do to prevent that exception from being thrown? You can see that
the following code uses Response.End. is there a better way?


------------------- start of code snippet ---------------------

System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;

response.Clear();
response.Charset = "";
response.ContentEncoding = System.Text.Encoding.UTF8;
response.Charset = "";
response.AddHeader("Content-Disposition", "attachment;filename=\"" +
filename + "\"");

// get the text of the rendered datagrid
string dgText;
using (StringWriter sw = new StringWriter())
{
using (System.Web.UI.HtmlTextWriter htw = new
System.Web.UI.HtmlTextWriter(sw))
{
  // instantiate a datagrid
  dg.RenderControl(htw);
  dgText = sw.ToString();
}
}

// set the response mime type
response.ContentType = "application/vnd.ms-excel";
response.Write(dgText);
response.End();

------------------- end of code snippet ---------------------
Any ideas?

Thanks

Author
16 Dec 2005 10:53 PM
Jose Rodriguez
Jeff. I ran into the same issue not long ago,  it is documented here

http://support.microsoft.com/default.aspx?scid=kb;en-us;312629

on my exception class, I added this code to bypass it

If AppException.GetBaseException.GetType.Name = "ThreadAbortException" Then

Return

End If

hth

Jose
Show quote
"Jeff" <Jeff@NoSpam.com> wrote in message
news:OAFojnoAGHA.356@TK2MSFTNGP12.phx.gbl...
>I have some code that enables users to download the contents of a DataGrid
>as an Excel file. The following code snippet is the relevant part. It works
>just fine - meaning specifically that the downloaded Excel file is what I
>want.
>
> The problem is that every time this code runs, my "catch all" exception
> handler (an HTTP Module) catches the "Thread was being aborted" exception
> (from System.Threading.Thread.AbortInternal()).
>
> What can I do to prevent that exception from being thrown? You can see
> that the following code uses Response.End. is there a better way?
>
>
> ------------------- start of code snippet ---------------------
>
> System.Web.HttpResponse response =
> System.Web.HttpContext.Current.Response;
>
> response.Clear();
> response.Charset = "";
> response.ContentEncoding = System.Text.Encoding.UTF8;
> response.Charset = "";
> response.AddHeader("Content-Disposition", "attachment;filename=\"" +
> filename + "\"");
>
> // get the text of the rendered datagrid
> string dgText;
> using (StringWriter sw = new StringWriter())
> {
> using (System.Web.UI.HtmlTextWriter htw = new
> System.Web.UI.HtmlTextWriter(sw))
> {
>  // instantiate a datagrid
>  dg.RenderControl(htw);
>  dgText = sw.ToString();
> }
> }
>
> // set the response mime type
> response.ContentType = "application/vnd.ms-excel";
> response.Write(dgText);
> response.End();
>
> ------------------- end of code snippet ---------------------
> Any ideas?
>
> Thanks
>
Author
16 Dec 2005 11:28 PM
Jeff
Thanks Jose!

AddThis Social Bookmark Button