B
brett.mack
Hello! I'm trying to update a text box from a thread. It works fine
until my thread is created from a seperate class. To illustrate, here's
my form code (the textbox is txtOutput.text) and for the class:
(This all works fine if the "Count" class is declared simply as a
function within form1 - but outside of that the control updates are
invisible!
Imports System.Threading
Delegate Sub SetTextCallback(ByVal [text] As String)
Public Class Form1
Dim tClass As count
Dim t As Thread
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnStart.Click
tClass = New count
t = New Thread(AddressOf tClass.bProcess)
t.Start()
While t.IsAlive
Thread.Sleep(20)
End While
End Sub
Public Sub SetText(ByVal [text] As String)
' InvokeRequired required compares the thread ID of the
' calling thread to the thread ID of the creating thread.
' If these threads are different, it returns true.
If Me.txtOutput.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf SetText)
Me.Invoke(d, New Object() {[text]})
Else
Me.txtOutput.Text = [text] & txtOutput.Text
Me.txtOutput.Refresh()
End If
End Sub
Private Sub btnAbort_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnAbort.Click
t.Abort()
End Sub
End Class
And here's the class for the count class:
Public Class count
Inherits Form1
Public i As Integer
Sub New()
i = 0
End Sub
Sub bProcess()
For i = 1 To 1000
'If i Mod 25 = 0 Then
' SetText(i.ToString & " " & ControlChars.CrLf)
' 'txtOutput.Text = i.ToString & " " & ControlChars.CrLf
'Else
SetText(i.ToString & " ")
'End If
Next
End Sub
End Class
until my thread is created from a seperate class. To illustrate, here's
my form code (the textbox is txtOutput.text) and for the class:
(This all works fine if the "Count" class is declared simply as a
function within form1 - but outside of that the control updates are
invisible!
Imports System.Threading
Delegate Sub SetTextCallback(ByVal [text] As String)
Public Class Form1
Dim tClass As count
Dim t As Thread
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnStart.Click
tClass = New count
t = New Thread(AddressOf tClass.bProcess)
t.Start()
While t.IsAlive
Thread.Sleep(20)
End While
End Sub
Public Sub SetText(ByVal [text] As String)
' InvokeRequired required compares the thread ID of the
' calling thread to the thread ID of the creating thread.
' If these threads are different, it returns true.
If Me.txtOutput.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf SetText)
Me.Invoke(d, New Object() {[text]})
Else
Me.txtOutput.Text = [text] & txtOutput.Text
Me.txtOutput.Refresh()
End If
End Sub
Private Sub btnAbort_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnAbort.Click
t.Abort()
End Sub
End Class
And here's the class for the count class:
Public Class count
Inherits Form1
Public i As Integer
Sub New()
i = 0
End Sub
Sub bProcess()
For i = 1 To 1000
'If i Mod 25 = 0 Then
' SetText(i.ToString & " " & ControlChars.CrLf)
' 'txtOutput.Text = i.ToString & " " & ControlChars.CrLf
'Else
SetText(i.ToString & " ")
'End If
Next
End Sub
End Class