I’m sure it’s a simple problem. I read from serial port some data and at some parse point, I need to update some statuslabel. Surprisingly, statuslabel is threadsafe… I didn’t know…
Ok, but the statuslabel needs to include some combo text which appears to be not thread safe
statLabel1.Text = “Connected to ” & cmbPort.text & “, found and happy”
So I tried to make a function to “Read Safe” from any component.text but I failed…
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
Can someone give a little help?
I think your problem is that you’re not assinging anything to
swhenInvokeRequired = True.You can assign the result to
s(you’ll need to make thatInvokeinstead ofBeginInvoke), but it might be better to invoke the event handler that you’re calling this from instead. You didn’t post that code, but it is basically the same idea — test thatInvokeRequiredon the form, then callBeginInvokefor the event handler itself.