我该如何解决这个跨线程错误?
How can I solve this cross thread error?
跨线程操作无效:控制文本框从创建它的线程以外的线程访问。 vb.net
Sub autocompletepayee()
Dim cmd2 As New Odbc.OdbcCommand
Dim dr As Odbc.OdbcDataReader
Dim myquery As String
myquery = "select payee from tbrequest"
cmd2.CommandText = myquery
cmd2.Connection = con
dr = cmd2.ExecuteReader
While dr.Read
txtPayee.AutoCompleteCustomSource.Add(dr.GetString(0)) 'this is the line where the error prompt
End While
dr.Close()
这应该可行,但如果更多地了解代码所在的位置,它可能会更短
Dim cmd2 As New Odbc.OdbcCommand
Dim dr As Odbc.OdbcDataReader
Dim myquery As String
myquery = "select payee from tbrequest"
cmd2.CommandText = myquery
cmd2.Connection = con
dr = cmd2.ExecuteReader
While dr.Read
If txtPayee.InvokeRequired Then
txtPayee.Invoke(Sub()
txtPayee.AutoCompleteCustomSource.Add(dr.GetString(0)) 'this is the line where the error prompt
End Sub)
Else
txtPayee.AutoCompleteCustomSource.Add(dr.GetString(0)) 'this is the line where the error prompt
End If
End While
dr.Close()
跨线程操作无效:控制文本框从创建它的线程以外的线程访问。 vb.net
Sub autocompletepayee()
Dim cmd2 As New Odbc.OdbcCommand
Dim dr As Odbc.OdbcDataReader
Dim myquery As String
myquery = "select payee from tbrequest"
cmd2.CommandText = myquery
cmd2.Connection = con
dr = cmd2.ExecuteReader
While dr.Read
txtPayee.AutoCompleteCustomSource.Add(dr.GetString(0)) 'this is the line where the error prompt
End While
dr.Close()
这应该可行,但如果更多地了解代码所在的位置,它可能会更短
Dim cmd2 As New Odbc.OdbcCommand
Dim dr As Odbc.OdbcDataReader
Dim myquery As String
myquery = "select payee from tbrequest"
cmd2.CommandText = myquery
cmd2.Connection = con
dr = cmd2.ExecuteReader
While dr.Read
If txtPayee.InvokeRequired Then
txtPayee.Invoke(Sub()
txtPayee.AutoCompleteCustomSource.Add(dr.GetString(0)) 'this is the line where the error prompt
End Sub)
Else
txtPayee.AutoCompleteCustomSource.Add(dr.GetString(0)) 'this is the line where the error prompt
End If
End While
dr.Close()