Results 1 to 3 of 3

Thread: Thread problems

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2007
    Posts
    86

    Thread problems

    Forgive me if I sound a bit stupid

    I am using the DotNetBar componant 'SuperTab' which is basically a tab control
    (Not really relevent but Im setting the scene)

    I have 5 seperate 'Subs' that create a tab and display a crystalreportviewer

    These are working great individually, but when I run all 5 on the form load, it obviously takes a few seconds for everything to run.

    So... I thought I would have a go at my first 'threat' and see if I can get all 5 to run symultaniously.

    Im using seperate Backgroundworkers, and for each one the 'RunWorkerAsync calls the individual Subs to get all 5 to run

    I am hitting across the error
    'Control accessed from a thread other than the thread it was created on.'
    when each worker is called

    Ive done some googling, and tried a number things but I cannot get it to work.

    The one that seemed the most successful was:

    Code:
    Private Sub CreateInvoiceOnlyTab()
    
            If Me.InvokeRequired Then
                Me.Invoke(New MethodInvoker(AddressOf CreateInvoiceOnlyTab))
            Else
    
    'Rest of the Sub code goes here
    But when that runs it tells me it cant call a disposed object.

    So now Im stuck

    This is the code without any threading elements:

    Code:
    Private Sub CreateInvoiceOnlyTab()
    
            Dim newTab As SuperTabItem = SuperTabControl1.CreateTab("Standard Invoice")
            Dim panel As SuperTabControlPanel = DirectCast(newTab.AttachedControl, SuperTabControlPanel)
            Dim crviewer As New CrystalReportViewer
           
            panel.Controls.Add(crviewer)
            crviewer.Dock = DockStyle.Fill
            crviewer.ToolPanelView = ToolPanelViewType.None
    
            'Generate Report
            Dim report As DaMtechInvoice = New DaMtechInvoice
            Dim crParameterDiscreteValue As ParameterDiscreteValue
            Dim crParameterFieldDefinitions As ParameterFieldDefinitions
            Dim crParameterFieldLocation As ParameterFieldDefinition
            Dim crParameterValues As ParameterValues
    
            '
            ' Get the report parameters collection.
            '
            'crystalreportspartsviewer()
            crParameterFieldDefinitions = report.DataDefinition.ParameterFields
    
            ' Add a parameter value - START
            crParameterFieldLocation = crParameterFieldDefinitions.Item("PARAM1")
            crParameterValues = crParameterFieldLocation.CurrentValues
            crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue
            crParameterDiscreteValue.Value = InvoiceID
            crParameterValues.Add(crParameterDiscreteValue)
            crParameterFieldLocation.ApplyCurrentValues(crParameterValues)
            ' Add a parameter value - END
    
            crParameterFieldLocation = crParameterFieldDefinitions.Item("PARAM2")
            crParameterValues = crParameterFieldLocation.CurrentValues
            crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue
            crParameterDiscreteValue.Value = InvoiceID
            crParameterValues.Add(crParameterDiscreteValue)
            crParameterFieldLocation.ApplyCurrentValues(crParameterValues)
    
            crParameterFieldLocation = crParameterFieldDefinitions.Item("PARAM3")
            crParameterValues = crParameterFieldLocation.CurrentValues
            crParameterDiscreteValue = New CrystalDecisions.Shared.ParameterDiscreteValue
            crParameterDiscreteValue.Value = ClientID
            crParameterValues.Add(crParameterDiscreteValue)
            crParameterFieldLocation.ApplyCurrentValues(crParameterValues)
    
            crviewer.ReportSource = report
            
        End Sub
    This code runs perfectly when called as a normal sub

    Thanks in advance

    Matt

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Thread problems

    You aren't supposed to do any UI based stuff in a background worker. The BGW runs on its own thread, so you can't work with stuff on the UI thread. Instead you are supposed to use the ProgressChanged and RunWorkerCompleted events, which fire on the UI thread, to make UI changes and updates as the BG worker progresses through its background work.

    MSDN explicitly states:
    You must be careful not to manipulate any user-interface objects in your DoWork event handler. Instead, communicate to the user interface through the ProgressChanged and RunWorkerCompleted events.

  3. #3
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Thread problems

    You are trying to work with controls in your thread: if you are working with controls, do it on the main (UI) thread that the controls are created on. If it takes time, determine where the time is being taken: is it retrieving data?

    I haven't worked with crystal reports for some time, but way back when, it was a heavy control which doesn't (didn't?) render very quickly. It looks like, though, you may be able to 'create' parts of the component (report) in a thread, then pass those parts back to the main thread and add to the viewer.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width