使用多线程vb.net时更新文本框

使用多线程vb.net时更新文本框,vb.net,multithreading,Vb.net,Multithreading,我的代码 我希望它显示文本框所有代理格式 Imports System.IO Public Class Form1 Dim thread As System.Threading.Thread Dim thread2 As System.Threading.Thread Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click thread = New System.Threading

我的代码

我希望它显示文本框所有代理格式

Imports System.IO
Public Class Form1
Dim thread As System.Threading.Thread
Dim thread2 As System.Threading.Thread
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    thread = New System.Threading.Thread(AddressOf getproxy)
    thread.Start()
End Sub
Private Sub getproxy()
    Try
        Dim ip As String = "76.125.85.66:16805 | 0.238 | Little Rock | AR | Unknown | United States69.207.212.76:49233 | 0.274 | Sayre | PA | 18840 | United States96.42.127.190:25480 | 0.292 | Sartell | MN | 56377 | United States"
        For Each m As Match In Regex.Matches(ip, "(?:\d{1,3}\.){3}\d{1,3}:\d+")
            TextBox1.Text += (m.Value) & vbNewLine
        Next
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub
但这是一个错误


{“跨线程操作无效:控件“TextBox1”是从创建它的线程以外的线程访问的。}

一旦启动线程,就无法从UI线程访问控件。您可以打开UI线程并更新文本框

76.125.85.66:16805
69.207.212.76:49233
96.42.127.190:25480

PS为什么你需要一个线程来完成这项工作?看起来这只需要很少的时间就能完成。线程是为了长时间的进程工作。

getproxy()中是否有更多的工作在进行

无论如何,您应该只调用实际更新UI的代码。其余代码应在另一个线程中运行:

Private Sub getproxy()
    Dim ip As String = "76.125.85.66:16805 | 0.238 | Little Rock | AR | Unknown | United States69.207.212.76:49233 | 0.274 | Sayre | PA | 18840 | United States96.42.127.190:25480 | 0.292 | Sartell | MN | 56377 | United States"
    Me.Invoke(Sub() 
     For Each m As Match In Regex.Matches(ip, "(?:\d{1,3}\.){3}\d{1,3}:\d+")         
      TextBox1.Text += (m.Value) & Enviroment.NewLine
     Next
               End Sub)
End Sub 
Private Sub getproxy()
    Try
        Dim ip As String = "76.125.85.66:16805 | 0.238 | Little Rock | AR | Unknown | United States69.207.212.76:49233 | 0.274 | Sayre | PA | 18840 | United States96.42.127.190:25480 | 0.292 | Sartell | MN | 56377 | United States"
        For Each m As Match In Regex.Matches(ip, "(?:\d{1,3}\.){3}\d{1,3}:\d+")

            ' ... possibly some other work done with "m.Value" ...

            ' Invoke just the updating of the UI itself:
            TextBox1.Invoke(Sub()
                                TextBox1.AppendText(m.Value & vbNewLine)
                            End Sub)
        Next
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub