gpt4 book ai didi

vb.net - 多线程安全调用未填充组合框vb.net

转载 作者:行者123 更新时间:2023-12-03 13:00:30 25 4
gpt4 key购买 nike

只是对我在这里做错了什么感到好奇,该原理应该起作用。有人可以帮我吗?

该代码运行良好,但似乎未将其添加到我的组合框中

正常的线程开始像这样

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,这里是带有调用方法之一的工作代码****

starting thread in another modul


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

Code within the Modul


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

最佳答案

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

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

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

关于vb.net - 多线程安全调用未填充组合框vb.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17306028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com