扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:QQ新人类 来源:YESKY 2007年11月10日
关键字:
Option Strict Off 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 Dim evs As ManualResetEvent ' Cast the state into the event evs = state.ResetEvent ' Set the priority and name Thread.CurrentThread.Priority = ThreadPriority.BelowNormal Try If Not state.ThreadName Is Nothing Then Thread.CurrentThread.Name = state.ThreadName End If Catch e As Exception ' Thread name can only be set once ' Don't set it and get out End Try 'Console.WriteLine("Listen {0} ", state.ThreadName) Try ' Set the path property on the MessageQueue object, assume private in this case objMQ.Path = mstrMachine & "\private$\" & mstrQueue ' Repeat until Interrupt received While True Try ' Sleep in order to catch the interrupt if it has been thrown Thread.CurrentThread.Sleep(100) ' Set the Message object equal to the result from the receive function ' Will block for 1 second if a message is not received objMsg = objMQ.Receive(New TimeSpan(0, 0, 0, 1)) ' Message found so signal the event to say we're working evs.Reset() ' Processing the message ProcessMsg(objMsg) ' Done processing Catch e As ThreadInterruptedException ' Catch the ThreadInterrupt from the main thread and exit Exit While Catch excp As MessageQueueException ' Catch any exceptions thrown in receive ' Probable timeout Finally ' Console.WriteLine("Setting Event " & Thread.CurrentThread.GetHashCode()) ' Done with this iteration of the loop so set the event evs.Set() End Try ' If finished then exit thread If mFinished Then 'console.WriteLine("exiting " & thread.CurrentThread.GetHashCode) Exit While End If End While Catch e As ThreadInterruptedException ' Catch the ThreadInterrupt from the main thread and exit End Try End Sub Private Sub ProcessMsg(ByVal pMsg As Message) ' Here is where we would process the message End Sub Public Sub Monitor() Dim intItem As Integer Dim objState As EventState ReDim mEvs(mWorkItems) mFinished = False 'Console.WriteLine("Queuing {0} items to Thread Pool", mWorkItems) For intItem = 0 To mWorkItems - 1 'Console.WriteLine("Queue to Thread Pool {0}", intItem) mEvs(intItem) = New ManualResetEvent(False) objState = New EventState(mEvs(intItem), "Worker " & intItem) ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf Me.Listen), _ objState) Next End Sub Public Sub Finish(Optional ByVal pTimeout As Integer = 0) 'Console.WriteLine("Waiting for Thread Pool to drain") ' Make sure everyone gets through the last iteration mFinished = True ' Block until all have been set If pTimeout = 0 Then WaitHandle.WaitAll(mEvs) ' Waiting until all threads signal that they are done. Else WaitHandle.WaitAll(mEvs, pTimeout, True) End If 'Console.WriteLine("Thread Pool has been drained (Event fired)") End Sub End Class |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者