title: Closing a rendering report from ReportViewer sometimes hangs application
body: <p>I am having an issue when trying to close a report that is still rendering in the ReportViewer inside a VB.NET application. When I try to close the still rendering report (report with over 600 pages) it will SOMETIMES seemingly close the report but hang the entire application (inclusing the minimize/maximise functions) but still show it as "Running" in task manager. </p>
  
  <p>I have implemented the <code>CancelRendering</code> Function but this seems to only help in situations where the report was going to close properly in the first place. I have tried all sorts of different values for the timeout including "-1" and "0" but nothing makes a difference. The only constant thing with this error is when the report hangs the application the <code>CancelRendering</code> returns "<code>False</code>". </p>
  
  <p>Here is a snippet of the ReportBase class that is runnning the WinForms.ReportViewer:</p>
  
  <pre><code>Private Sub ReportBase_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
      Dim parent As MainForm
      parent = Me.ParentForm ' Get the parent form of this child
  
      ' If there is only one child left and this child is closing then show the explorer bar
      If parent.MdiChildren.GetLength(0) = 1 Then
          parent.UltraExplorerBar1.Show()
          parent.HomeToolStripMenuItem.Visible = False
          MainForm.Panel2.Visible = True
      End If
  
  End Sub
  
  Private Sub ReportBase_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
      Dim millisecondsTimeout As Integer = 100 'waits for the report to cancel rendering
      Dim returnValue As Boolean
      returnValue = ReportViewer2.CancelRendering(millisecondsTimeout)
      MessageBox.Show(returnValue.ToString)
  End Sub
  
  
  Private Sub ReportBase_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      With ReportViewer2
          'Get reports from remote server
  
          .ProcessingMode = Microsoft.Reporting.WinForms.ProcessingMode.Remote
          .ServerReport.ReportServerUrl = New System.Uri(ReportServer)
  
      End With
  End Sub
  </code></pre>
  
  <p><strong>EDIT 1:</strong> 
  Something I forgot to mention. I checked my threads and the "Rendering" thread remaind open when the report hangs my application. </p>
  
  <p>Also when CancelRendering is set to "-1" the report never closes. The application just hangs with the report still open.</p>
  
  <p><strong>EDIT 2:</strong>
  This is only happening when I open the report in "<code>Printlayout</code>" aka <code>ReportViewer.SetDisplayMode(Microsoft.Reporting.WinForms.DisplayMode.PrintLayout)</code>. When the report is opened normally it loads all 413 pages in an instant. When loaded in "<code>PrintLayout</code>" the report loads each page (over 600 in this mode) one by one very slowly. I think it has something to do with exiting the thread that is loading each of these pages. </p>
  
  <p>I could not get the error when loading into the regular mode.</p>
  
  <p>I would run a profiler session on the report server but unfortunatly I do not have access at this time. If I do gain access I will update with the results.</p>
  
  <p><strong>EDIT 3:</strong>
  When the report closes correctly (and the CancelRendering works properly) I get "Report processing was cancelled" and <code>CancelRendering</code> returns true</p>
  
  <p><img src="https://i.stack.imgur.com/j3o8t.png" alt="works"></p>
  
  <p>When the report does not stop rendering before closing, it keeps rendering after the <code>CancelRendering</code> fires and returns a False.</p>
  
  <p><img src="https://i.stack.imgur.com/uDyVX.png" alt="does not work"></p>
  
  <p>Another thing I discovered is that if I close the entire application instead of just the report, The application will close but stay open only as a process in Task Manager. this makes me think it really is a issue closing the Render thread. The threads also do not have locations when the application is hanging.</p>
  
  <p><img src="https://i.stack.imgur.com/Hh1kv.png" alt="threads"></p>