title: processing of page stops after response.writefile
body: <p>This question has been asked a number of times but still I am not able to find a proper solution to it. I have two questions.</p>
  
  <p>I am creating an excel file, writing data to it and then rendering it to the user for download. I have used microsoft.interop for the same.</p>
  
  <p>The first problem is:
  I am trying to use a messagebox(javascript) to show how many rows were generated in the excel file after the user has downloaded the excel. This messagebox never shows, however the code is executed. How can i achieve this?</p>
  
  <p>Secondly:
  After the excel has been created, an excel.exe process keeps running in the task manager, even after i have manually cleaned up the references.</p>
  
  <p>This alert box does not show after the file is rendered.</p>
  
  <pre><code>flag = WriteDatasetToExcel(ds);
              if (flag)
              {
  
                  ClientScript.RegisterClientScriptBlock(GetType(), "Javascript", "&lt;script&gt;alert('Number of rows generated : " + ds.Tables[0].Rows.Count + "')&lt;/script&gt;");
              }
  </code></pre>
  
  <p>The relevant code is given</p>
  
  <pre><code>excelApp = new Excel.Application();
          excelwrkbook = excelApp.Workbooks.Add(1);
          excelwrksheet = (Excel.Worksheet)excelwrkbook.Sheets[1];
          excelwrksheet.Name = "Application Data";
  </code></pre>
  
  <p>after writing data, saving and rendering is done like this:</p>
  
  <pre><code>excelwrkbook.SaveAs(filepath, Excel.XlFileFormat.xlWorkbookNormal, Missing.Value, Missing.Value, false, true, Excel.XlSaveAsAccessMode.xlNoChange, Excel.XlSaveConflictResolution.xlLocalSessionChanges, false, Missing.Value, Missing.Value, true);
          excelwrkbook.Close(0, 0, 0);
          HttpContext.Current.Response.Clear();
          HttpContext.Current.Response.Buffer = true;
          HttpContext.Current.Response.Charset = "";
          HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + filename);
          HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
          HttpContext.Current.Response.WriteFile(filepath);
          excelApp.Quit();
          flag = true;
  </code></pre>
  
  <p>and now finally the cleanup code:</p>
  
  <pre><code>ReleaseObjects(excelrange);
          ReleaseObjects(excelwrksheet);
          ReleaseObjects(excelwrkbook);
          ReleaseObjects(excelApp);
  </code></pre>
  
  <p>the releaseobjects function is as follows:</p>
  
  <pre><code>private void ReleaseObjects(Object obj)
  {
      if (obj != null &amp;&amp; Marshal.IsComObject(obj))
      {
          while (System.Runtime.InteropServices.Marshal.ReleaseComObject(obj) != 0) { }
  
      }
  }
  </code></pre>