' Enter critical section Monitor.Enter(mcnSQL) ' Alternate code ' Do While Not Monitor.TryEnter(mcnSQL) ' Thread.CurrentThread.Sleep(100) ' Loop sdrIns = cmSQL.ExecuteReader() Catch e As Exception End Try
Do While sdrIns.Read ' Read the data and write it to a binary stream Loop
Imports System Imports System.Threading Imports System.Messaging Imports Microsoft.VisualBasic
Public Class QueueListener ' Used to listen for MSMQ messages
Protected Class EventState ' Used to store the event and any other state data required by the listener Public ResetEvent As ManualResetEvent Public ThreadName As String
Public Overloads Sub New(ByVal myEvent As ManualResetEvent) MyBase.New() ResetEvent = myEvent End Sub
Public Overloads Sub New(ByVal myEvent As ManualResetEvent, ByVal Name As String) MyBase.New() ResetEvent = myEvent ThreadName = Name End Sub End Class
Private mstrMachine As String Private mstrQueue As String Private mWorkItems As Integer = 7 Private mFinished As Boolean = False Dim mEvs() As ManualResetEvent
Public Property WorkItems() As Integer Get Return mWorkItems End Get Set(ByVal Value As Integer) If Value > 15 Then mWorkItems = 15 Else mWorkItems = Value End If End Set End Property
Public Sub New(ByVal Machine As String, ByVal Queue As String) ' Constructor accepts the necessary queue information mstrMachine = Machine mstrQueue = Queue End Sub
Public Sub Listen(ByVal state As Object) ' Method that each thread uses to listen for messages
' Create a MessageQueue object Dim objMQ As System.Messaging.MessageQueue = New System.Messaging.MessageQueue() ' Create a Message object Dim objMsg As System.Messaging.Message ' = New System.Messaging.Message() ' Event from the state