im trying to make a simple example in threading this error msg
can anyone help me to solve it ?

Cross-thread operation not valid: Control 'cmb1' accessed from a thread other than the thread it was created on.

Imports System.Threading

Public Class Form1

    Inherits System.Windows.Forms.Form

    Dim th1 As Thread
    Dim th2 As Thread

    Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
        th1 = New Thread(AddressOf proc1)
        th2 = New Thread(AddressOf proc2)
        th1.Start()
        th2.Start()
    End Sub

    Sub proc1()
        Dim iCount As Integer
        For iCount = 1 To 10
            cmb1.Items.Add(iCount)
        Next
    End Sub

    Sub proc2()
        Dim iCount As Integer
        For iCount = 11 To 20
            cmb2.Items.Add(iCount)
        Next
    End Sub

End Class

Recommended Answers

All 2 Replies

You have to use a delegate to modify controls. It's not complicated and there is a tutorial on how to use BackGround threads which includes a discussion of delegates. Once you have read the tutorial feel free to post any follow up questions here.

thank you very much :)

Private Delegate Sub proc1Delegate()
Private Delegate Sub proc2Delegate()

and 

 If Me.InvokeRequired Then

 ....

 else 
 '
 ...
make the program work correctly ^_^ 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.