Windows Develop Bookmark and Share   
 index > Windows Forms General > Invoke Procedures
 

Invoke Procedures

Hello,


I am writing a program that assigns data sources to crystal report documents and assigns them to crystal report viewer on a Windows form. To make the UI responsive I chose to implement this inside new threads. My Sub does some database binding and then assigns the datasource to the crystal viewer which is naturally running in the UI main thread.

As usual, I had to use delegates and invoke. What I am doing is that I am checking whether the viewer is invoked before modifying its data source.


Please find below my code:

Code Snippet

Private Sub ShowAllInternationalCalls()



Dim myDoc as New CrystalDecisions.CrystalReports.Engine.ReportDocument
MyDoc= New rpt_rated_allcalls()


'Fill Data Adapter Code
EmpTableAdapter.FillBy_All_Employees(Me.DataSet.Employees)


'Set Report Datasource Property
rptDoc.SetDataSource(DataSet)

'Assign the dataset to the report. Since this SUB is running in a separate thread from the UI. It must
'be(passed) to the UI main thread through a delegate.
If CrystalViewer.InvokeRequired Then

Dim newDelegate As New UIDelegate(AddressOf ShowAllInternationalCalls)
CrystalViewer.Invoke(newDelegate)

Else

CrystalViewer.ReportSource = rptDoc

End If

End Sub


Now my problem is that I noticed that every time an invoke is performed the SUB is called again and so doing the same job twice. This applies on modification that I might want to do on CrystalViewer. Is there a way to avoid this? Thanks
Leedo  Thursday, June 14, 2007 9:51 AM
Setting CheckForIllegalCrossThreadCalls to False is very bad advice. Sooner or later (usually later) your program will crash and burn. You simply need another subroutine that just assigns the CrystalView.ReportSource property and nothing else. Right now, you execute all of the statements in ShowAllInternationalCalls() again in the UI thread.
nobugz  Thursday, June 14, 2007 4:12 PM

Yes! Just Call CheckForIllegalCrossThreadCalls before asking if invoke is required an don't use it

Private Sub ShowAllInternationalCalls()



Dim myDoc as New CrystalDecisions.CrystalReports.Engine.ReportDocument
MyDoc= New rpt_rated_allcalls()

EmpTableAdapter.FillBy_All_Employees(Me.DataSet.Employees)

rptDoc.SetDataSource(DataSet)

CheckForIllegalCrossThreadCalls = False


CrystalViewer.ReportSource = rptDoc


End Sub

If you prefer to do it the way you want (checking if invoke is required)

then refactor this part of code

If CrystalViewer.InvokeRequired Then

Dim newDelegate As New UIDelegate(AddressOf ShowAllInternationalCalls)
CrystalViewer.Invoke(newDelegate)

Else

CrystalViewer.ReportSource = rptDoc

End If

to a new method.

Westor  Thursday, June 14, 2007 12:27 PM
Setting CheckForIllegalCrossThreadCalls to False is very bad advice. Sooner or later (usually later) your program will crash and burn. You simply need another subroutine that just assigns the CrystalView.ReportSource property and nothing else. Right now, you execute all of the statements in ShowAllInternationalCalls() again in the UI thread.
nobugz  Thursday, June 14, 2007 4:12 PM
Yeah you are right. I agree that this might not be safe when working with multi-threading. Anyway, I figured that I should include all the code that I want to run once in the ELSE section after checking for CrystalView control IsInvoked event. I believe this is the best technique, what do you think.

Regards..

Leedo  Sunday, June 17, 2007 6:47 AM

You can use google to search for other answers

Custom Search

More Threads

• ToolTips and MicroSoft Calculator (XP)
• Automate BackSpace key
• Sorting a TreeView Control
• Cannot access TreeNode.Value property
• Display extended ASCII chars in listview
• Strongly typed databinding?
• Linking treeview with textBox
• Scroll position to active panel
• Stange Checkbox Behavior: Text disappears when AutoEllipsis should appear
• Textbox and button visible true or false problem