473,788 Members | 2,820 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exception: Thread was being aborted

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.Threadin g.Thread.AbortI nternal()).

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.Http Response response = System.Web.Http Context.Current .Response;

response.Clear( );
response.Charse t = "";
response.Conten tEncoding = System.Text.Enc oding.UTF8;
response.Charse t = "";
response.AddHea der("Content-Disposition", "attachment;fil ename=\"" +
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.H tmlTextWriter(s w))
{
// instantiate a datagrid
dg.RenderContro l(htw);
dgText = sw.ToString();
}
}

// set the response mime type
response.Conten tType = "applicatio n/vnd.ms-excel";
response.Write( dgText);
response.End();

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

Thanks
Dec 16 '05 #1
2 5180
Jeff. I ran into the same issue not long ago, it is documented here

http://support.microsoft.com/default...b;en-us;312629

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

If AppException.Ge tBaseException. GetType.Name = "ThreadAbortExc eption" Then

Return

End If

hth

Jose
"Jeff" <Je**@NoSpam.co m> wrote in message
news:OA******** *****@TK2MSFTNG P12.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.Threadin g.Thread.AbortI nternal()).

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.Http Response response =
System.Web.Http Context.Current .Response;

response.Clear( );
response.Charse t = "";
response.Conten tEncoding = System.Text.Enc oding.UTF8;
response.Charse t = "";
response.AddHea der("Content-Disposition", "attachment;fil ename=\"" +
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.H tmlTextWriter(s w))
{
// instantiate a datagrid
dg.RenderContro l(htw);
dgText = sw.ToString();
}
}

// set the response mime type
response.Conten tType = "applicatio n/vnd.ms-excel";
response.Write( dgText);
response.End();

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

Thanks

Dec 16 '05 #2
Thanks Jose!
Dec 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6792
by: Jan | last post by:
Hi, If I put a try catch block for Server.Transfer call. It will catch a System.Threading.ThreadAbortException exception like this. However, it transfer the page to the correct page. Why did this exception occur? Thanks. ------------------------------------ {"Thread was being aborted." }
1
1769
by: Vi | last post by:
Hi, I have a try block in which I execute a Page.Response.Redirect("myPage.aspx"); This statement always generates the exception: "Thread was being aborted" and the execution continues in the catch block. Is there a way to execute Page.Response.Redirect without generating this exception? Or if not, is there at least a way to ignore this exception in the code? Thank you.
5
5751
by: Lucvdv | last post by:
Can someone explain why this code pops up a messagebox saying the ThreadAbortException wasn't handled? The first exception is reported only in the debug pane, as expected. The second (caused by thread.Abort()) is reported twice: once in the debug window, and once through the message box. Is it because the thread was sleeping when the exception occurred?
5
2031
by: Alan Silver | last post by:
Hello, I have a page that is supposed to do some checking, and if OK, set a session variable before redirecting to another page. The following code is a simplified version, I have hard-coded the basket ID and removed a load of extra checking... public partial class GoToCheckout : Page { public void Page_Load(Object o, EventArgs e) { try {
3
1819
by: Nick | last post by:
Hi there, This probably wont make much sense but I'm getting an unhandled exception being thrown in the following line... Try While True Redraw control as necessary End While Catch ex as Exception <<< Here!
4
2902
by: R.A.M. | last post by:
Hello, Could you help me plase? I have an ASP.NET page with "Search" button; when button is clicked Search_Click is called; here's the code: protected void Search_Click(object sender, EventArgs e) { Debug.WriteLine("WWWPage.Search_Click()"); ... try
1
3018
by: R.A.M. | last post by:
Hello, Could you help me plase? I am describing my problem second time because I haven't got a solution. I have an ASP.NET page with "Search" button; when button is clicked Search_Click is called; here's the code: protected void Search_Click(object sender, EventArgs e) { Debug.WriteLine("WWWPage.Search_Click()"); ...
4
4921
by: Totto | last post by:
Hi, I'm doing a server.transfer from a click event of a button, but an exception is raised with "Thread was being aborted" Anyone know why? Thanks Tor
10
2828
by: =?Utf-8?B?ZHNoZW1lc2g=?= | last post by:
Hello, I have a C# project with windows forms. I sometimes get unprdictable .NET exceptions. I don't always get them, so I can't reproduce where exactly it happens. I can generally say it happens when closing the application (maybe in the dispose method?). Here are 2 examples: System.Threading.ThreadAbortException: Thread was being aborted. at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.