New to Threads

hog

Senior Contributor
Joined
Mar 17, 2003
Messages
984
Location
UK
I'm trying to export a report to pdf whilst opening and displaying the report to the user. This is causing a long delay before the user can do anything with the report view.

I have had a go at creating threads but get the following error message when I run the code below:

"Controls created on one thread cannot be parented to a control on a different thread"

Visual Basic:
    Private Sub CrystalReportViewer1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles CrystalReportViewer1.Load

        Try

            Select Case gintReportType

                Case gconstReportMailShot

                    myReport = New repMailShot
                    myReport.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Portrait

                Case constReturns

                    ' myReport = New repReturns
                    myReport.PrintOptions.PaperOrientation = CrystalDecisions.Shared.PaperOrientation.Portrait

            End Select

            myReport.SetDataSource(m_dsReportSource)

            Dim thdCopyPDF As New Thread(AddressOf CopyToPDF)
            Dim thdLoadReport As New Thread(AddressOf LoadReport)

            thdLoadReport.Start()
            thdCopyPDF.Start()

        Catch objException As Exception

            ShowError("Location:   Class frmReports" & ControlChars.CrLf & ControlChars.CrLf & "Procedure: " & _
                      "CrystalReportViewer1_Load(ByVal sender As Object, ByVal e As System.EventArgs)" & _
                      ControlChars.CrLf & ControlChars.CrLf & "Error Text: " & objException.Message)

        End Try

    End Sub

    Private Sub CopyToPDF()

        myReport.ExportToDisk(CrystalDecisions.[Shared].ExportFormatType.PortableDocFormat, gstrServerArea & "Mailshot.pdf")

    End Sub

    Private Sub LoadReport()

        Try

            Me.CrystalReportViewer1.ReportSource = myReport

            Me.CrystalReportViewer1.Zoom(75)

        Catch objException As Exception

            ShowError("Location:   Class frmReports" & ControlChars.CrLf & ControlChars.CrLf & "Procedure: " & _
                      "LoadReport" & ControlChars.CrLf & ControlChars.CrLf & "Error Text: " & objException.Message)

        End Try

    End Sub

Have I misunderstood thread usage?
 
Ok after reading the tutor corner on this site and followed the links there I have now added this code:

Visual Basic:
    Delegate Sub ChangeLoadDelegate()

    Me.CrystalReportViewer1.Invoke(New ChangeLoadDelegate(AddressOf LoadReport))

But I still get the same error :(
 
Have a look at the article below. It will tell you what to do.

SUMMARY
When large queries to a database are executed, the application may become unresponsive for a long period of time. To avoid this behavior and decrease the waiting time of the user, the query can be executed on a background thread, releasing the application for other tasks until the data is returned from the database and databinding is performed.

This step-by-step article demonstrates how to query a database on a background thread and use databinding to display the results in a DataGrid object.

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:

Microsoft Visual Studio .NET
Access to the Northwind sample database

Background
By design, Windows Form or Control methods cannot be called on a thread other than the one that created the form or control. If you attempt to do this, an exception is thrown. Depending on the exception handling implemented in your code, this exception may cause your application to terminate. If no exception handling is implemented, the following error message is displayed:
An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll

Additional information: Controls created on one thread cannot be parented to a control on a different thread.
The exception is raised because Windows Forms are based on a single-threaded apartment (STA) model. Windows Forms can be created on any thread; after they are created, however, they cannot be switched to a different thread. In addition, the Windows Form methods cannot be accessed on another thread; this means that all method calls must be executed on the thread that created the form or control.

Method calls that originate outside the creation thread must be marshalled (executed) on the creation thread. To do this asynchronously, the form has a BeginInvoke method that forces the method to be executed on the thread that created the form or control. The synchronous method call is done with a call to the Invoke method.

back to the top
Build the Windows Forms Application
This section describes how to create a Windows Form application that queries a database on a background thread and uses the BeginInvoke method to perform databinding on a DataGrid.
Start Visual Studio .NET.
Create a new project, select Visual C# as the Project Type, and use the Windows Application template.
Add a Button object to the form, and change its Text property to "Query on Thread".
Add another Button to the form, and change its Text property to "Query on Form".
Add a Label to the form, and clear its Text property.
Add a TextBox to the form.
Add a DataGrid to the form.
Right-click the form, and then click View Code; this displays the code of your application.
Add the following import statements to the top of the page to import the Threading and SqlClient namespaces.
Imports System.Threading
Imports System.Data.SqlClient

Add the following code immediately below the Windows Forms Designer Generated Code:
Dim UpdateThread As Thread
Dim UpdateThreadStart As New ThreadStart(AddressOf QueryDataBase)
Dim CallDataBindToDataGrid As New MethodInvoker(AddressOf Me.DataBindToDataGrid)

Dim MyDataSet As DataSet
Dim MyDataAdapter As SqlDataAdapter
Dim MyQueryString As String = "SELECT Products.* FROM [Order Details] CROSS JOIN Products"
Dim MyConnection As New SqlConnection("data source=localhost;initial catalog=northwind;integrated security=SSPI;")

NOTE: The query used in this demonstration is a Cartesian Product that returns over 165,000 rows from the Northwind database. The amount of data returned is large so that the responsiveness of the form can be demonstrated.
Open the Windows Forms Design View.
Double-click the Query on Thread button, and paste the following code in the Click event for this button:
UpdateThread = New Thread(UpdateThreadStart)
UpdateThread.IsBackground = True
UpdateThread.Name = "UpdateThread"
UpdateThread.Start()

Open the Design view again, and then double-click the Query on Form button. Paste the following code in the Click event for this button:
QueryDataBase()

Paste the following code below the button events that you added in the earlier steps:

' Sub routine that is to be executed on Form's thread.
Public Sub DataBindToDataGrid()
DataGrid1.DataSource = MyDataSet
DataGrid1.DataMember = "MyTable"
MyDataAdapter = Nothing
MyDataSet = Nothing
End Sub

' Sub routine used by the background thread to query database.
Public Sub QueryDataBase()
MyDataSet = New DataSet()
MyConnection.Open()
Dim cmd As New SqlCommand(MyQueryString, MyConnection)
MyDataAdapter = New SqlDataAdapter(cmd)
Label1.Text = "Filling DataSet"
MyDataAdapter.Fill(MyDataSet, "MyTable")
MyConnection.Close()
Label1.Text = "DataSet Filled"
' Make asynchronous function call to Form's thread.
Me.BeginInvoke(CallDataBindToDataGrid)
End Sub

These Sub routines are used by the background thread to query the database and databind it to the DataGrid located on the Windows Form when the first button is clicked. The Click event of the second button calls the QueryDataBase Sub routine directly, and will be executed on the Windows Form thread.
Press CTRL+SHIFT+B to build your application.
back to the top
Demonstration
To see the benefit that is gained by using a background thread to query the database, follow these steps:
Press CTRL+F5 to execute your application without debugging.

Click the Query on Form button. This begins the query on the Windows Forms thread. If you then try to enter some text in the text box that is displayed on the form, the application does not respond. After the query has completed (this may take some time, depending on your computer), the DataGrid displays the results of the query.

Click the Query on Thread button. This creates a background thread that queries the database and keeps the application responsive to user interaction. To see this, click the button, and then type some text in the text box on the form.
back to the top
 
Back
Top