Console Application (VBThreadingMonitor)

Introduction

The following example shows how synchronization can be accomplished using the VB SyncLock keyword and the Pulse method of the Monitor object. The Pulse method notifies a thread in the waiting queue of a change in the object's state (for more details on pulses, see the Monitor.Pulse Method).

Running the Sample

Using the Code

The example creates a Cell object that has two methods: ReadFromCell and WriteToCell. Two other objects are created from classes CellProd and CellCons; these objects both have a method ThreadRun whose job is to call ReadFromCell and WriteToCell. Synchronization is accomplished by waiting for "pulses" from the Monitor object, which come in order. That is, first an item is produced (the consumer at this point is waiting for a pulse), then a pulse occurs, then the consumer consumes the production (while the producer is waiting for a pulse), and so on.

Cell Class

VB
Edit|Remove
Public Class Cell
       Public Function ReadFromCell() As Integer
           SyncLock Me
               If Not Me.readerFlag Then
                   Try
                       Monitor.Wait(Me)
                   Catch e As SynchronizationLockException
                       Console.WriteLine(e)
                   Catch e As ThreadInterruptedException
                       Console.WriteLine(e)
                   End Try
               End If
               Console.WriteLine("Consume: {0}", Me.cellContents)
               Me.readerFlag = False
               Monitor.Pulse(Me)
           End SyncLock
           Return Me.cellContents
       End Function


       Public Sub WriteToCell(ByVal n As Integer)
           SyncLock Me
               If Me.readerFlag Then
                   Try
                       Monitor.Wait(Me)
                   Catch e As SynchronizationLockException
                       Console.WriteLine(e)
                   Catch e As ThreadInterruptedException
                       Console.WriteLine(e)
                   End Try
               End If
               Me.cellContents = n
               Console.WriteLine("Produce: {0}", Me.cellContents)
               Me.readerFlag = True
               Monitor.Pulse(Me)
           End SyncLock
       End Sub




       ' Fields
       Private cellContents As Integer
       Private readerFlag As Boolean = False
   End Class

 

Cell Prod Class

VB
Edit|Remove
Public Class CellProd
       ' Methods
       Public Sub New(ByVal box As Cell, ByVal request As Integer)
           Me.cell = box
           Me.quantity = request
       End Sub


       Public Sub ThreadRun()
           Dim looper As Integer = 1
           Do While (looper <= Me.quantity)
               Me.cell.WriteToCell(looper)
               looper += 1
           Loop
       End Sub




       ' Fields
       Private cell As Cell
       Private quantity As Integer = 1
   End Class

 

Cell Cons Class

VB
Edit|Remove
Public Class CellCons
        ' Methods
        Public Sub New(ByVal box As Cell, ByVal request As Integer)
            Me.cell = box
            Me.quantity = request
        End Sub


        Public Sub ThreadRun()
            Dim looper As Integer = 1
            Do While (looper <= Me.quantity)
                Dim valReturned As Integer = Me.cell.ReadFromCell
                looper += 1
            Loop
        End Sub




        ' Fields
        Private cell As Cell
        Private quantity As Integer = 1
    End Class

 

More Information

         Monitor Pulse