I was assuming that if I didn't do this, the main thread would just
suck all of the processor iterating through the while loop; I guess now
that isn't correct? Sorry, I've been teaching myself on the fly, no
formal programming education (I'm sure it shows)!
The below code works.
Question: On the main form code "SetText" routine, should I uncomment
the Monitor.Enter(me) & Monitor.Exit(me) when I have more than one
background thread running at the same time, updating the textbox?
What I've done so far is this. Main form code follows:
Imports System.Threading
Public 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
btnStart.Enabled = False
tClass = New count
t = New Thread(AddressOf tClass.bProcess)
t.Start()
btnStart.Enabled = True
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.
'Monitor.Enter(Me)
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
'Monitor.Exit(Me)
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 in the "Count" class code, I've put this:
Imports system.threading
Public Class count
Public i As Integer
Dim f1 As Form1
Dim st As SetTextCallback
Sub New()
i = 0
st = New SetTextCallback(AddressOf Form1.SetText)
End Sub
Sub bProcess()
For i = 1 To 1000
st(i.ToString & " ")
Thread.Sleep(10)
Next
End Sub
End Class
> Hell,
>
[quoted text clipped - 89 lines]
> >> > End Sub
> >> > End Class
Henning Krause [MVP - Exchange] - 09 Jan 2007 14:39 GMT
Hello,
I would assume that setting a text value is not a thread safe operaiton, so
using a Monitor here is ok. But you should use the SyncLock keyword in
Visual Basic, as it will wrap the Monitor.Exit in a finally block.
Best regards,
Henning Krause
>I was assuming that if I didn't do this, the main thread would just
> suck all of the processor iterating through the while loop; I guess now
[quoted text clipped - 158 lines]
>> >> > End Sub
>> >> > End Class
Lasse Vågsæther Karlsen - 10 Jan 2007 12:51 GMT
> I was assuming that if I didn't do this, the main thread would just
> suck all of the processor iterating through the while loop; I guess now
[quoted text clipped - 5 lines]
> the Monitor.Enter(me) & Monitor.Exit(me) when I have more than one
> background thread running at the same time, updating the textbox?
<snip>
>>>>> Public Sub SetText(ByVal [text] As String)
>>>>> ' InvokeRequired required compares the thread ID of the
[quoted text clipped - 8 lines]
>>>>> End If
>>>>> End Sub
<snip>
You do not need the monitor for this method, the way it is above is
fine. Messages to the main thread are processed one at a time.

Signature
Lasse Vågsæther Karlsen
mailto:lasse@vkarlsen.no