none
How to Cancel Report rendering asynchronously? RRS feed

  • Question

  • Hi
     
    I am trying to find out how to cancel rendering of a report viewer control when it is running in a different thread.

    Currently the reportviewer is loading on a form aysnchronously, however if the user attempts to close the form whilst a report is rendering the application crashes with an unhandled exception.

    Code is shown below:

    Private Delegate Sub RefreshReportDelegate()
    Private
    _execRptDel As StartReportExecutionDelegate = New StartReportExecutionDelegate(AddressOf ExecuteReport)

    Private Sub frmReportViewer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Try
        Me.Cursor = Cursors.WaitCursor
        ' Start asynch process
    _    execRptDel.BeginInvoke(Nothing, Nothing)
    Catch ex As Exception
        HandleException(ex)
    Finally
        Me.Cursor = Cursors.Default
    End Try
    End Sub

    Private Sub ExecuteReport()

    Try
        Dim rptRow As DataRowView = Nothing
        
    If ReportViewer.InvokeRequired Then
            
    <<code to initialise report here>>
            serverReport.SetParameters(_parameters)
            
    ' Refresh report asynch
            Dim rrDel As RefreshReportDelegate = New RefreshReportDelegate(AddressOf ExecuteReport)
            Me.BeginInvoke(rrDel)
    Else
            
    ' Set the processing mode for the ReportViewer to Remote
            ReportViewer.ProcessingMode = ProcessingMode.Remote
            ' Run report
            ReportViewer.RefreshReport()
    End If

    Catch ex As Exception
        HandleException(ex)
    End Try

    End Sub

    Private Sub frmReportViewer_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

    Try
        ReportViewer.CancelRendering(0)  ***THIS WILL NOT WORK FOR ASYNCHRONOUS EXECUTION***
    Catch ex As Exception
        HandleException(ex)
    End Try

    End Sub




    The aysnchronous execution was implemented to allow the form window time to load properly.

    If the user closes the form before the report has finished rendering I am calling ReportViewer.CancelRendering via the FormClosing event - this works fine for synchronous execution but not with the new async method. How do I get back to the thread the report viewer is running on to cancel the rendering and prevent an unhandled exception? have tried replacing the FormCLosing code with something like below but it still doesn't work

    'If ReportViewer.InvokeRequired Then

    ' Dim cancelDel As CancelRenderingDelegate = New CancelRenderingDelegate(AddressOf CancelRendering)

    ' Me.BeginInvoke(cancelDel)

    'Else

    ' CancelRendering()

    'End If


    Any help appreciated! 

      
    • Edited by GeordieBoy Monday, August 11, 2008 10:02 AM title clarity
    Monday, August 11, 2008 9:57 AM

All replies

  • Your call to CancelRendering is non-blocking - it times out after 0 milliseconds.  If you want to ensure that the report is cancelled when CancelRendering returns, you should be calling CancelRendering(System.Threading.Timeout.Infinite).

    Friday, August 15, 2008 11:08 PM
    Moderator
  • thanks Brian,  I've implmeneted the infinite timeout and so far it seems to be working.

    One question - I've also now re-instated my asynchronous solution and have this code in the form closing event, 


    If
    ReportViewer.InvokeRequired Then
        Dim cancelDel As CancelRenderingDelegate = New CancelRenderingDelegate(AddressOf CancelRendering)
        Me.BeginInvoke(cancelDel)
    Else
        
    CancelRendering()
    End If

    where

    Private Sub CancelRendering()
        ReportViewer.CancelRendering(System.Threading.Timeout.Infinite)
    End Sub


    is that valid? It's working although when I debug it never seems to return true for ReportViewer.InvokeRequired ie is always executing the call synchronously - in which case am wondering if I need to bother to check.


     

    Tuesday, August 19, 2008 10:32 AM
  • Hi,

    did you got any solution for the above problem. currently i'm facing a problem while cancelling a report having bulky data. CancelRendering(0) or CancelRendering(-1) is returning false and the application is getting hanged. how to cancel the background process runing? The same report is working fine while accessing it through Report manager I.E browser.

     

    Could anyone help me in this regard.

    Tuesday, December 20, 2011 9:34 AM