I think the best way is "create and add controls on main thread". And here
what we do in multi-threading.

Delegate Sub AddControlDelegate(methodParameters)

Sub AddControl(methodParameters)
    If Me.InvokeRequired Then
        Me.Invoke(New AddControlDelegate(AddressOf AddControl),
methodParameters)
    Else
        ' Create New Control, use methodParameters
        ' Add control in container
    End If
End Sub

And instead of using following code,

If Me.InvokeRequired Then
     ' It's on a different thread, so use Invoke.
    Me.Invoke(Me.Controls.Add(Ctrl1))  <----------------- not sure what to
here
Else
    Me.Ribbon1.Controls.Add(Ctrl1)
End If
Just execute the AddControl() method with parameters

Regards,
Arsalan Tamiz


On Fri, Apr 17, 2009 at 5:28 PM, Barkingmadscot <[email protected]> wrote:

>
> I am try to add controls to my program , which i have no problem
> doing.  The problem is i want to improve the speed/preformance of my
> App, i have delcare and get a number of thread to complete work. one
> of which is to add x number of controls to the form.
>
>
> this is the sub called by the thread
>
>      Private Sub mythreadmethod1()
>
>        CentralSQL.SQLString = "Exec spTC_ResourceGroups '" &
> strProjNo & "'"
>
>        Dim xPos As Integer = 1
>        For Each Group As String In CentralSQL.Read(1, SQLConn)
>            If Group = "" Then Group = "UnAssigned"
>
>            Ctrl1 = New CustomControl
>            Ctrl1.Header.Text = Group
>
>            If xPos > 1 Then
>                Ctrl1.Location = New Point((Ctrl1.Width * (xPos - 1))
> + 3, 1) '# position next ctrl to the perviously control
>            Else
>                Ctrl1.Location = New Point(1, 1)
>            End If
>
>            If Me.InvokeRequired Then
>                ' It's on a different thread, so use Invoke.
>                Me.Invoke(Me.Controls.Add(Ctrl1))  <-----------------
> not sure what to here
>            Else
>                Me.Ribbon1.Controls.Add(Ctrl1)
>            End If
>            xPos += 1
>        Next
>
>    End Sub
>

Reply via email to