Multi Threading and VB.Net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • richkid
    New Member
    • Apr 2007
    • 28

    Multi Threading and VB.Net

    hi..

    i am fairly new to VB.Net and I have a form thats calling a crystal report. The report however takes a long time to load. i tried using Threads and I keep getting the following error : [ controls created on one thread cannot be parented to a control on another thread ] how can i solve this error. Any assistance is greatly appreciated

    Code:
    
        Public docReport As New rptStatus
        Public invReport As New rptInventory
        Public useReport As New rptUser
        Public oReport As Object
        Public rType as String
    
    
      Private Sub frmReports_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            If rType = "Inventory" Then
                oReport = invReport
            ElseIf rType = "Status" Then
                oReport = docReport
            Else
                oReport = useReport
                oReport.SetParameterValue(0, param3)
                oReport.SetParameterValue(1, param4)
                oReport.SetParameterValue(2, param1)
                oReport.SetParameterValue(3, param2)
            End If
    
            Dim repThread As New Thread(AddressOf loadReport)
            repThread.IsBackground = True
            repThread.Priority = ThreadPriority.Normal
            repThread.Start()
           
        End Sub
    
    
    
     Private Sub loadReport()
    
            Control.CheckForIllegalCrossThreadCalls = False
            cviewer.Visible = True
            Me.Cursor = Cursors.WaitCursor
            frmMain.tslCurrent.Text = "Loading Report ..."
            Application.DoEvents()
    
            Try
                Dim myTable As CrystalDecisions.CrystalReports.Engine.Table
                Dim myLogin As CrystalDecisions.Shared.TableLogOnInfo
    
                For Each myTable In oReport.Database.Tables
                    myLogin = myTable.LogOnInfo
                    myLogin.ConnectionInfo.Password = strPass
                    myLogin.ConnectionInfo.UserID = strUser
                    myLogin.ConnectionInfo.ServerName = strServer
                    myLogin.ConnectionInfo.DatabaseName = strDatabase
                    myTable.ApplyLogOnInfo(myLogin)
                Next
    
    
                '******************ERROR OCCURS HERE *******************************
                cviewer.ReportSource = oReport
                cviewer.ShowRefreshButton = False
    
            Catch ex As Exception
    
                MsgBox(ex.Message)
                frmMain.tslCurrent.Text = "Report not Loaded"
              
            Finally
                Me.Cursor = Cursors.Default
                'frmMain.tslCurrent.Text = ""
            End Try
        End Sub
  • jhaxo
    New Member
    • Dec 2007
    • 57

    #2
    In windows you can't access a gui element from any thread except the ui thread. Microsoft has provided something called delegates that your thread can invoke on the gui thread. A delegate is an object that can point to a function.

    Here is a tutorial
    http://www.codeproject .com/KB/vb/Delegate.aspx

    I looked for one on this site. I am sure there is one but I didn't see it.

    The basic idea is every Form has a method BeginInvoke so you need to :

    1. Define a delegate
    Code:
    public delegate void MyDelegate(string mssg); //in c#.
    2. instantiate a delegate
    Code:
    MyDelegate mydelegate = new ShowStatusDelegate(myfunction)
    3. define the function pointed to by the delegate
    Code:
    void myfunction(string txt)
    {
       textbox1.Text=txt;
    }
    4. now in your thread:
    Code:
    BeginInvoke(mydelegate,new object[]{"hello delegate world");

    Comment

    • richkid
      New Member
      • Apr 2007
      • 28

      #3
      hi..
      thanks for that post.... I tried it and I am now getting the report to show.. only problem is that Im back at the original problem because now I have to wait on the report to load before I can do anything :(

      Comment

      Working...