2

只是好奇我在这里做错了什么,原则应该有效。谁能帮我一把?

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

正常线程像这样开始

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

可能我怎么称呼代表?

任何帮助表示赞赏

在此处输入图像描述

****感谢@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
4

1 回答 1

2

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

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

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

于 2013-06-25T21:13:31.293 回答