嘿伙计我的问题是如何在线程中访问(更新/读取)richtextbox。 我刚刚创建了一个非常简单的代码,让您了解我在做什么。我在互联网上搜索了一些关于调用,委托或背景工作的文章,希望有人可以来告诉我哪些以及如何使用。非常感谢。
Imports System.Threading
Public Class form1
Dim flag As Boolean = True
Dim startbtn As Thread
Dim stopbtn As Thread
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
startbtn = New Thread(AddressOf startfuction)
startbtn.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
stopbtn = New Thread(AddressOf stopfunction)
stopbtn.Start()
End Sub
'************** thread 1
Private Sub startfuction()
flag = True
While flag = True
richtextbox1.text = "Your process started" 'error
End While
End Sub
'************** thread 2
Private Sub stopfunction()
flag = False
startbtn.Abort()
MsgBox("You ended the process")
End Sub
结束班
答案 0 :(得分:0)
Imports System.Threading
Public Class Form1
Dim flag As Boolean = True
Dim startbtn As Thread
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
startbtn = New Thread(AddressOf startfuction)
startbtn.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
flag = False
MsgBox("You ended the process")
End Sub
'************** thread 1
Private Sub startfuction()
flag = True
While flag = True
Me.Invoke(Sub() RichTextBox1.Text = "Your process started") 'error
End While
Me.Invoke(Sub() RichTextBox1.Text = "Your process stopped") 'error
End Sub
End Class
编辑1
同样在运行线程时,当您转到关闭应用程序时,您可能会遇到问题,因为您的线程会过早结束...
做类似
的事情Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
flag = False
Application.DoEvents()
End Sub