I am trying to set text to label Label_caller.Text = phone_number
and I get this error: "System.InvalidOperationException: Cross-thread operation not valid: Control 'Label_caller' accessed from a thread other than the thread it was created on." How do I overcome this problem? How do I use keyword Me.?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
In Windows, you can access UI elements only on the UI thread. For that reason, if you need to access them from another thread, you may need to invoke that action on the UI thread.
You need to use the following method to update the text box. This will check if invoking on the main thread is required and if needed, call the same method on the UI thread.
Private Sub UpdateTextBox(ByVal phone_number As String)
If Me.InvokeRequired Then
Dim args() As String = {phone_number}
Me.Invoke(New Action(Of String)(AddressOf UpdateTextBox), args)
Return
End IF
Label_caller.Text = phone_number
End Sub
回答2:
I am probably answering quite late but adding following code in form load event seems solving problem.
Not sure though it's perfect answer or not:
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False