0
我相信這是一個簡單的問題。我從串口讀取一些數據,並在一些解析點上,我需要更新一些狀態標籤。出人意料的是,statuslabel是線程安全的......我不知道......代表函數返回其他線程中的值
好,但statuslabel需要包括似乎不是線程安全的一些組合文本
statLabel1.Text =「連接到」 & cmbPort.text &「發現快樂」
於是,我就做一個函數從任何component.text‘讀取安全’,但我失敗了......
Private Delegate Function ControlGetTextDelegate(ByRef cmp As Control) As String
Public Function ControlGetText(ByRef cmp As Control) As String
Dim s As String = ""
' control invoked from another thread ?
If cmp.InvokeRequired Then ' YES, so call through delegate
Dim dlg As New ControlGetTextDelegate(AddressOf ControlGetText)
cmp.BeginInvoke(dlg, {cmp})
Else ' NO, so call normally
s = cmp.Text
End If
Return s
End Function
能有人給一點點幫幫我?
你搖滾:)非常感謝。我改爲s = cmp.Invoke(dlg,{cmp}),並且像魅力一樣工作。事實上,我試過,但BeginInvoke不允許返回我的價值。 – user1797147
你能解釋一下嗎? – user1797147
'BeginInvoke'是異步的,所以它返回'IAsyncResult'。在你的例子中你想要'調用'。或者在事件處理程序中使用'BeginInvoke'。 –