使用多线程 vb.net 时更新文本框 [英] update textbox when using multi thread vb.net

查看:126
本文介绍了使用多线程 vb.net 时更新文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码

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

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

i want it show textbox all proxy format

76.125.85.66:16805
69.207.212.76:49233
96.42.127.190:25480

但是报错

{跨线程操作无效:控件 'TextBox1' 从创建它的线程以外的线程访问."}

{"Cross-thread operation not valid: Control 'TextBox1' accessed from a thread other than the thread it was created on."}

推荐答案

一旦启动线程,您将无法从 UI 线程访问控制.您可以 调用 UI 线程并更新文本框.

You cannot access control from the UI thread once you start a thread. You can Invoke the UI thread and update the textbox.

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 

PS 为什么你需要一个线程来完成这项工作?似乎这需要很少的时间来执行.线程用于长时间的进程工作.

PS why do you need a thread for this work? Seems like this would take little time to perform. Threading is for long process work.

这篇关于使用多线程 vb.net 时更新文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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