MyGlobal.OutgoingEmailQueue.AddWork(新的 EmailObject(" jo *@hotmail.com",helo, joe)) 私人班级SingleThreadWorkQueue 私有workQueue As Queue = Queue.Synchronized(新队列())私有wp作为DoWork 私有wt作为线程 Public Delegate Sub DoWork(ByVal w As Object)公共事件ExceptionOccured(ByVal e As Exception) Public Sub New(ByVal WorkProc As DoWork) wp = WorkProc wt =新线程(AddressOf workerThreadProc) wt.IsBackground = True wt.Name =" worker thread for &安培; wp.​​Target.GetType.Name& "。" & wp.Method.Name wt.Start() End Sub 私有子workerThreadProc() SyncLock workQueue Monitor.Wait(workQueue,5000)同时workQueue.Count> 0 尝试 Dim w As Object = workQueue.Dequeue wp.Invoke(w) Catch ex As Exception RaiseEvent ExceptionOccured(ex) End试试 Loop 循环结束SyncLock End Sub Public Sub AddWork(ByVal work as Object) workQueue.Enqueue(work)如果Monitor.TryEnter(workQueue)那么 Monitor.Exit(workQueue)结束如果结束子 结束类 Here''s a type called SingleThreadWorkQueue for this kind of thing. When you create a SingleThreadWorkQueue is creates a thread and a Queue. The thread pulls items off the queue and then invokes a delegate (a function outside the type), and passes it whatever was on the queue. So you would just bundle up the information to send an email into an object, and pass it to SingleThreadWorkQueue.AddWork. The worker thread would then dequeue the work and invoke your work function. Like this: --- somewhere in global scope ---- public shared OutgoingEmailQueue as new SingleThreadWorkQueue(addressOf SendAnEmail) public shared sub SendAnEmail(work as Object) dim em as MyEmailInfo = directcast(work,MyEmailInfo) --send the email end sub ---- anywhere you want to generate an email, somehting like ---- MyGlobal.OutgoingEmailQueue.AddWork(new EmailObject("jo*@hotmail.com","helo, joe")) Private Class SingleThreadWorkQueue Private workQueue As Queue = Queue.Synchronized(New Queue()) Private wp As DoWork Private wt As Thread Public Delegate Sub DoWork(ByVal w As Object) Public Event ExceptionOccured(ByVal e As Exception) Public Sub New(ByVal WorkProc As DoWork) wp = WorkProc wt = New Thread(AddressOf workerThreadProc) wt.IsBackground = True wt.Name = "worker thread for " & wp.Target.GetType.Name & "." & wp.Method.Name wt.Start() End Sub Private Sub workerThreadProc() SyncLock workQueue Do Monitor.Wait(workQueue, 5000) Do While workQueue.Count > 0 Try Dim w As Object = workQueue.Dequeue wp.Invoke(w) Catch ex As Exception RaiseEvent ExceptionOccured(ex) End Try Loop Loop End SyncLock End Sub Public Sub AddWork(ByVal work As Object) workQueue.Enqueue(work) If Monitor.TryEnter(workQueue) Then Monitor.Pulse(workQueue) Monitor.Exit(workQueue) End If End Sub End Class 这篇关于单独的服务线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-01 18:43