多线程安全调用不会填充组合框vb.net

时间:2013-06-25 19:38:58

标签: vb.net multithreading

只是好奇我在这里做错了什么,原则应该有效。任何人都可以帮我一把吗?

代码运行正常,但似乎没有将它们添加到我的组合框中

普通线程就像这样开始

t1 = New Thread(New ThreadStart(AddressOf GetNewClientData))
t1.Start()

数据不为空或为空......:)

Function GetNewClientData()

    Try

        Dim con As New SqlConnection
        Dim myConString As String = My.Settings.ConString
        Dim objcommand As SqlCommand = New SqlCommand

        With objcommand
            .Connection = con
            Dim cmdText As String = "SELECT distinct Applicant,Client,Market,Project  from AAClient order by Client"
            .CommandText = cmdText

        End With

        con.ConnectionString = myConString
        con.Open()

        Using readerObj As SqlClient.SqlDataReader = objcommand.ExecuteReader
            'This will loop through all returned records 
            While readerObj.Read
                addClientInvoke(readerObj("Client").ToString)
                addApplicantInvoke(readerObj("Client").ToString)
                addMarketInvoke(readerObj("Client").ToString)
                addProjectInvoke(readerObj("Client").ToString)
            End While
        End Using

        con.Close()

    Catch ex As Exception

    End Try

    Return Nothing
End Function

Delegate Sub addApplicant(s As String)
Sub addApplicantInvoke(ByVal s As String)
    If CreateNewSite.cbApplicant.InvokeRequired Then
        Dim d As New addApplicant(AddressOf addApplicantInvoke)
        CreateNewSite.cbApplicant.Invoke(d, New Object() {s})
    Else
        CreateNewSite.cbApplicant.Items.Add(s)
    End If
End Sub

Delegate Sub addClient(s As String)
Sub addClientInvoke(ByVal s As String)
    If CreateNewSite.cbClient.InvokeRequired Then
        Dim d As New addClient(AddressOf addClientInvoke)
        CreateNewSite.cbClient.Invoke(d, New Object() {s})
    Else
        CreateNewSite.cbClient.Items.Add(s)
    End If
End Sub

Delegate Sub addMarket(s As String)
Sub addMarketInvoke(ByVal s As String)
    If CreateNewSite.cbMarket.InvokeRequired Then
        Dim d As New addMarket(AddressOf addMarketInvoke)
        CreateNewSite.cbMarket.Invoke(d, New Object() {s})
    Else
        CreateNewSite.cbMarket.Items.Add(s)
    End If
End Sub

Delegate Sub addProject(s As String)
Sub addProjectInvoke(ByVal s As String)
    If CreateNewSite.cbProject.InvokeRequired Then
        Dim d As New addProject(AddressOf addProjectInvoke)
        CreateNewSite.cbProject.Invoke(d, New Object() {s})
    Else
        CreateNewSite.cbProject.Items.Add(s)
    End If
End Sub

可能我怎么称呼代表?

感谢任何帮助

enter image description here

****感谢@jods这里是使用其中一个调用方法****

的工作代码
  

在另一个模块中启动线程

t1 = New Thread(New ParameterizedThreadStart(AddressOf GetNewClientData))
t1.Start(Me)
  

模块内的代码

Function GetNewClientData(ByVal oldForm As CreateNewSite)

    Try

        Dim con As New SqlConnection
        Dim myConString As String = My.Settings.ConString
        Dim objcommand As SqlCommand = New SqlCommand

        With objcommand
            .Connection = con
            Dim cmdText As String = "SELECT distinct Applicant,Client,Market,Project from AAClient order by Client"
            .CommandText = cmdText

        End With

        con.ConnectionString = myConString
        con.Open()

        Using readerObj As SqlClient.SqlDataReader = objcommand.ExecuteReader
            'This will loop through all returned records 
            While readerObj.Read
                addApplicantInvoke(readerObj("Applicant").ToString, oldForm)
                addClientInvoke(readerObj("Client").ToString)
                addMarketInvoke(readerObj("Market").ToString)
                addProjectInvoke(readerObj("Project").ToString)
            End While
        End Using

        con.Close()

    Catch ex As Exception
        MsgBox(ex)
    End Try

    Return Nothing
End Function

Delegate Sub addApplicant(s As String, oldform As CreateNewSite)
Sub addApplicantInvoke(ByVal s As String, ByVal oldform As CreateNewSite)
    If oldform.InvokeRequired Then
        Dim d As New addApplicant(AddressOf addApplicantInvoke)
        oldform.cbApplicant.Invoke(d, New Object() {s, oldform})
    Else
        oldform.cbApplicant.Items.Add(s)
    End If
End Sub

1 个答案:

答案 0 :(得分:2)

问题是CreateNewSite.cbProject。 CreateNewSite不是您的表单实例。这是一个很好的:p VB.NET功能,称为默认表单实例:

VB有一个“默认表单实例”的概念。对于应用程序命名空间中的每个Form,将在Forms属性下的My命名空间中创建一个默认实例。

您需要将正确的表单实例(即“我”/“this”)传递给后台主题。