雨浅听风吟 2019-05-24 09:54 采纳率: 0%
浏览 281

vb.net关于线程中修改界面文本框没有反应

#1 问题概述
图片说明

Imports System.Threading
Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim objTest1 As New Form1
        Dim thread1 As New Thread(New ThreadStart(AddressOf objTest1.count))
        thread1.Name = "线程1"
        Dim objTest2 As New Form1
        Dim thread2 As New Thread(New ThreadStart(AddressOf objTest2.count))
        thread2.Name = "线程2"
        Dim objTest3 As New Form1
        Dim thread3 As New Thread(New ThreadStart(AddressOf objTest3.count))
        thread3.Name = "线程3"
        thread1.Start()
        thread2.Start()
        thread3.Start()
    End Sub
    Public Sub count()
        Dim cnt As Integer = 1
        While cnt < 6

            cnt += 1
            TextBox1.Text += Thread.CurrentThread.Name + "数到" + Str(cnt) + vbCrLf
            Thread.Sleep(100)
        End While
    End Sub
End Class

为何运行无反应

  • 写回答

1条回答 默认 最新

  • 雨浅听风吟 2019-05-28 09:36
    关注

    这段代码不能实现主要有两个问题,这个跨线程了所以要使用invoke从主线程中修改ui,同时改完后如下会发现

    Imports System.Threading
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim objTest1 As New Form1
            Dim thread1 As New Thread(New ThreadStart(AddressOf objTest1.count1))
            thread1.Name = "线程1"
            Dim objTest2 As New Form1
            Dim thread2 As New Thread(New ThreadStart(AddressOf objTest2.count1))
            thread2.Name = "线程2"
            Dim objTest3 As New Form1
            Dim thread3 As New Thread(New ThreadStart(AddressOf objTest3.count1))
            thread3.Name = "线程3"
            thread1.Start()
            thread2.Start()
            thread3.Start()
        End Sub
        Public Sub count1()
            Dim cnt As Integer
            cnt = 0
            While cnt < 6
                cnt += 1
    
                Me.Invoke(New showDelegate(AddressOf show1), cnt)
                Application.DoEvents()
            End While
    
        End Sub
    
        Public Delegate Sub showDelegate(ByVal cnt As Integer)
        Public Sub show1(ByVal cnt As Integer)
            TextBox1.Text += Thread.CurrentThread.Name + "数到" + Str(cnt) + vbCrLf
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    End Class
    

    这时会发现
    图片说明
    这主要是由于objTest继承了form1的text box1的属性但是还并没有创建text box1
    这时将objTest1删除从form1调用即可成功
    代码如下

    Imports System.Threading
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim thread1 As New Thread(New ThreadStart(AddressOf count1))
            thread1.Name = "线程1"
            Dim objTest2 As New Form1
            Dim thread2 As New Thread(New ThreadStart(AddressOf count1))
            thread2.Name = "线程2"
            Dim objTest3 As New Form1
            Dim thread3 As New Thread(New ThreadStart(AddressOf count1))
            thread3.Name = "线程3"
            thread1.Start()
            thread2.Start()
            thread3.Start()
        End Sub
        Public Sub count1()
            Dim cnt As Integer
            cnt = 0
            While cnt < 6
                cnt += 1
    
                Me.Invoke(New showDelegate(AddressOf show1), cnt)
                Application.DoEvents()
            End While
    
        End Sub
    
        Public Delegate Sub showDelegate(ByVal cnt As Integer)
        Public Sub show1(ByVal cnt As Integer)
            TextBox1.Text += Thread.CurrentThread.Name + "数到" + Str(cnt) + vbCrLf
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    End Class
    

    但是线程名字还是无法显示,后来摸索了下在invoke中线程是主线程的名字,所以可以在while循环中添加一个变量来代表threadName
    代码如下

    Imports System.Threading
    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim thread1 As New Thread(New ThreadStart(AddressOf count1))
            thread1.Name = "线程1"
            Dim objTest2 As New Form1
            Dim thread2 As New Thread(New ThreadStart(AddressOf count1))
            thread2.Name = "线程2"
            Dim objTest3 As New Form1
            Dim thread3 As New Thread(New ThreadStart(AddressOf count1))
            thread3.Name = "线程3"
            thread1.Start()
            thread2.Start()
            thread3.Start()
        End Sub
        Public Sub count1()
            Dim cnt As Integer
            Dim threadName As String
            cnt = 0
            While cnt < 6
                cnt += 1
                threadName = Thread.CurrentThread.Name
                Me.Invoke(New showDelegate(AddressOf show1), threadName, cnt)
                Application.DoEvents()
            End While
    
        End Sub
    
        Public Delegate Sub showDelegate(ByVal threadName As String, ByVal cnt As Integer)
        Public Sub show1(ByVal threadName As String, ByVal cnt As Integer)
            TextBox1.Text += threadName + "数到" + Str(cnt) + vbCrLf
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    End Class
    
    

    结果

    这是可已完成目的,也可完全掌握过程中出现的错误
    图片说明

    评论

报告相同问题?

悬赏问题

  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝