简体   繁体   中英

label not updated in System.Threading.ThreadAbortException

In my page i am trying to download file. THe file is downloaded successfully but i get System.Threading.ThreadAbortException. So i handled that in my try Catch Block and set the error lable to blank but it doesnt get updated in page.

        catch (System.Threading.ThreadAbortException)
        {
            lblErrorMsg.Text = "dfdf";
        }
        catch (Exception ex)
        {
            lblErrorMsg.Text = "Error while processing you request :<br> Erorr Description : " + ex.Message;
        }

This is my Write file function

  public static void WriteFile(string nameOfFile, string fileContent, HttpResponse writer)
    {
        writer.ClearHeaders();
        writer.ClearContent();

        writer.ContentType = "text/xml";
        writer.AppendHeader("Content-Disposition", "attachment; filename=" + nameOfFile);
        writer.Write(fileContent);
        writer.Flush();
        writer.End();
    }

Can someone tell why label is not set to blank even though it comes under the Catch Block of system.thread.threadabortexceptiopn when i debug code ?

ThreadAbortException happens because you close the Response prematurely, by calling End() method of Response object. This also explains why it's too late to write on the page content. It's not a very annoying error but it would be better to handle it cleanly.

Just check these answers Why Response.Redirect causes System.Threading.ThreadAbortException? or How to Avoid Response.End() "Thread was being aborted" Exception during the Excel file download and other answers related to Response and ThreadAbortException to understand it and handle properly by writing a better code for file download, according your usage.

Also please note that doesn't make a great sense to have both a completely rewritten Response stream for a page and some content on it, like a Label.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM