从不是在其上创建线程的线程访问vb.net [英] vb.net accessed from a thread other than the thread it was created on

查看:126
本文介绍了从不是在其上创建线程的线程访问vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将文本设置为标签Label_caller.Text = phone_number,但出现此错误:"System.InvalidOperationException:跨线程操作无效:控件'Label_caller'从创建该线程的线程之外的其他线程访问."我该如何克服这个问题?如何使用关键字我"?

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.?

推荐答案

在Windows中,您只能在UI线程上访问UI元素.因此,如果您需要从另一个线程访问它们,则可能需要在UI线程上调用该操作.

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.

您需要使用以下方法来更新文本框.这将检查是否需要在主线程上调用,并且如果需要,请在UI线程上调用相同的方法.

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

这篇关于从不是在其上创建线程的线程访问vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆