Quote Originally Posted by dbasnett View Post
Code:
    Dim go As Long = 0
    Dim ct As Long = 0
    Dim lock As New Object
    Dim running As Long
    Const _toRun As Integer = 20 'try different numbers

    Private Sub Button2_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) Handles Button2.Click
        Button2.Enabled = False
        Label1.Text = DateTime.Now.ToLongTimeString 'set up
        Threading.Interlocked.Exchange(go, 0L)
        Threading.Interlocked.Exchange(ct, 0L)
        Threading.Interlocked.Exchange(running, 0)
        For x As Integer = 1 To _toRun 'start some threads
            Dim t As New Threading.Thread(AddressOf foo)
            t.Start()
            Threading.Thread.Sleep(10) 'give each one a chance to start
        Next
        Do 'wait for all to start
            Threading.Thread.Sleep(10)
        Loop While _toRun <> Threading.Interlocked.Read(running)

        Button3.Enabled = True
        Threading.Interlocked.Exchange(go, 1L) 'GO!
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) Handles Button3.Click
        Threading.Interlocked.Exchange(go, 0L) 'STOP
        Button3.Enabled = False
        aTimer.Interval = 100
        aTimer.Start()
    End Sub

    Dim WithEvents aTimer As New Timer()

    Private Sub _Tick(ByVal sender As System.Object, _
                            ByVal e As System.EventArgs) Handles aTimer.Tick
        If Threading.Interlocked.Read(running) > 0 Then Exit Sub
        aTimer.Stop()
        Label1.Text = DateTime.Now.ToLongTimeString
        Button2.Enabled = True
    End Sub

    Private Sub foo()
        Dim myID As Long = Threading.Interlocked.Increment(ct) 'get myID
        Threading.Interlocked.Increment(running)
        'wait for all threads to be started
        Do While Threading.Interlocked.Read(go) = 0L
            Threading.Thread.Sleep(10) 'sleep if not
        Loop
        Do
            Threading.Monitor.Enter(lock) 'lock
            updUI(myID) 'update the UI passing in myID <<< Update UI
            Threading.Monitor.Exit(lock) 'unlock
            Threading.Thread.Sleep(100) 'comment this out and see the difference
        Loop While Threading.Interlocked.Read(go) = 1L
        Threading.Interlocked.Decrement(running)
    End Sub

    Delegate Sub Stub(ByVal aCount As Long)
    Public myDelegate As Stub

    Private Sub updUI(ByVal theCount As Long)
        If Label1.InvokeRequired Then 'on UI thread
            myDelegate = New Stub(AddressOf updUI) 'no
            Label1.Invoke(myDelegate, theCount) 'this will run on the UI
            Exit Sub
        End If
        Dim s As String = Space(CInt(theCount)) & CLng(theCount).ToString
        Label1.Text = s
        Label1.Refresh()
    End Sub
Are you able to explain:-
Threading.Interlocked.Read
Threading.Monitor.Enter
Threading.Monitor.Exit

With my previous example, all my threads would run in parallel. How does the above example work? It seems at the very least to lock around the UI updating?