UI Freeze despite using Delegates!!

leedo2k

Active member
Joined
Nov 9, 2006
Messages
28
Programming Experience
Beginner
Hi,

I am using delegates and Invokerequired to marshal to the UI a call to my UI update function. However, despite using this usual scenario, my UI still freezes. Here is my code:

VB.NET:
Public Delegate UIDelegate   
   
    Private Sub ShowReport()   
        If Me.InvokeRequired Then   
            Dim newDelegate As New UIDelegate(AddressOf ShowReport)   
            Me.BeginInvoke(newDelegate)   
        Else   
            Dim rpt as  CrystalDecisions.CrystalReports.Engine.ReportDocument  
            rpt = new myCrystalReport 'A CrystalReport report in my project  
            Me.CrystalReportViewer1.ReportSource = rpt  
       End If   
  End Sub   
   
Private Sub GO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GO.Click   
  Dim t As New Thread(New ThreadStart(AddressOf ShowReport))   
  t.IsBackground = True   
  t.Start()   
End Sub

Please help me find out what is it that I am doing wrong here.. Thanks
 
What you are doing is completely pointless. In your Click event handler you are creating a new thread and executing the ShowReport method on that thread. The first thing the ShowReport method does delegate right back to the UI thread, so ShowReport gets executed a second time, this time on the UI thread, and that's where the work gets done. The whole point of using worker threads is to do the work there instead of on the UI thread, but you're doing all the work on the UI thread anyway.

What you should be doing is creating the report on the worker thread, assuming that that's the time-consuming operation, and then delegating back to the UI thread in order to show it.
VB.NET:
Private Sub GO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GO.Click
    Dim t As New Thread(New ThreadStart(AddressOf CreateReport))
    t.IsBackground = True
    t.Start()
End Sub

Private Sub CreateReport()
    Dim rpt As New myCrystalReport 'A CrystalReport report in my project

    Me.ShowReport(rpt)
End Sub

Private Delegate Sub ShowReportInvoker(ByVal rpt As CrystalDecisions.CrystalReports.Engine.ReportDocument)

Private Sub ShowReport(ByVal rpt As CrystalDecisions.CrystalReports.Engine.ReportDocument)
    If Me.InvokeRequired Then
        Me.Invoke(New ShowReportInvoker(AddressOf ShowReport), rpt)
    Else
        Me.CrystalReportViewer1.ReportSource = rpt
    End If
End Sub
Note that this will only be of any use if this:
VB.NET:
Dim rpt As New myCrystalReport
is the time-consuming operation. If it's actually this:
VB.NET:
Me.CrystalReportViewer1.ReportSource = rpt
that takes all the time then your multi-threading is pointless.
 
Thanks for the hint. You are right about the last part, the time consuming task will not just be crating an instance of the report. I also do a query against a typed dataset and assign the result to the report. This is what takes sometime.
I'll try out your suggestion.
 
Back
Top