0

Hey guys my question is how can I access (update/read) richtextbox in a thread. I just created a very simple code for you to understand what I am doing. I searched some articles on internet mentioned about invoke, delegate or backgroundworker, hope someone can come and tell me which and how to use. Really thanks.

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

End Class

Eric Lease
  • 4,114
  • 1
  • 29
  • 45
Dogemike
  • 3
  • 1
  • The problem with marking such a common question as a duplicate, at this point, is trying to track down the original. Don't feel like doing that right now. You are on the right track with invoke in my opinion, but there are several ways this could be done. Here is [one SO post](http://stackoverflow.com/a/12503832) that addresses your question rather precisely, but is really a duplicate of older questions, as it has also been marked as a possible duplicate. Also, adding the tag of the language you're dealing with is very helpful when trying to get a response. – Eric Lease Jan 17 '16 at 16:21
  • oh, sorry for that. I just think their code/question is too complex for newbies to read and understand. so i just create a very simple one for every beginner to understand. really thanks you do not delete this post. – Dogemike Jan 18 '16 at 01:28

1 Answers1

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

EDIT 1

Also when running threads, When you goto close your app you could run into problems because your threads will end prematurely..

do something like

Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    flag = False
    Application.DoEvents()
End Sub
myekem
  • 139
  • 8