Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net Visual Basic更新后台线程上的GUI.text属性_Vb.net_Multithreading_Background Thread - Fatal编程技术网

Vb.net Visual Basic更新后台线程上的GUI.text属性

Vb.net Visual Basic更新后台线程上的GUI.text属性,vb.net,multithreading,background-thread,Vb.net,Multithreading,Background Thread,我对VisualBasic有点生疏,但是我需要将它用于一个需要.NET语言的项目 当我在WindowsFourm应用程序中编写一些代码来更新文本框的文本值时,我注意到了这一点 'weve read the stuff...now lets update the textbox with the ADC value DataQSingleDevice.GetInterleavedBinaryData(BinaryData, 0, 18) DataQSingleDevic

我对VisualBasic有点生疏,但是我需要将它用于一个需要.NET语言的项目

当我在WindowsFourm应用程序中编写一些代码来更新文本框的文本值时,我注意到了这一点

   'weve read the stuff...now lets update the textbox with the ADC value
    DataQSingleDevice.GetInterleavedBinaryData(BinaryData, 0, 18)



    DataQSingleDevice.Stop()
    DATAQHandler(0).Disconnect()




    'now lets throw data in the textbox
    Button1.Text = "Connected!"

    For incramenter As Integer = 0 To 10
        TextBox1.Text = BinaryData(incramenter)
        Threading.Thread.Sleep(2000)
    Next
    end sub
当我在上面遍历这个for循环时,它不会在每次迭代时更新文本值。我假设这意味着它只能在这个sub所在的方法完成之后才能执行

我记得在Java中的Android编程中,像这样的属性修改通常在主UI线程上实现,而主UI线程深深地埋在一个永无止境的for循环中,只有上帝本人和Java语言的发明者才有希望找到这个循环。我还记得AsyncTask和Java.util.concurrent等方法允许我在后台线程上执行操作并更新某些视图

我的问题:


是否有一种方法可以在GUI(如“TextBox1.Text”)上更新visualBasic中的内容属性,类似于Android编程中的某些视图可以使用后台线程定期更新?(此过程可能需要几分钟的更新时间…此示例仅持续20秒,但我的实际使用可能持续10分钟)

实际上,有一种正确的方法可以从另一个线程访问UI。您应该这样做:

TextBox1.Invoke(Sub() TextBox1.Text = BinaryData(incramenter))

与此相反:

TextBox1.TextBox1.Text = BinaryData(incramenter)

.Invoke
.BeginInvoke
之间的区别在于第一个线程将同步运行,也就是说,当前线程将等待lambda子线程中的操作在UI线程中完全执行,然后才会继续。后者将发送要在UI线程中执行的lambda子线程,而不等待它运行,因此调用方线程将立即进行。由您选择一个更适合您的。

如果这是在后台线程中运行的,您必须调用BeginInvoke以强制在UI线程上进行更新。我将创建一个后台线程并尝试此操作!
TextBox1.TextBox1.Text = BinaryData(incramenter)