R
Random
I have a thread that I want to start, to process items waiting in a Queue
class, and run in a loop until that Queue is empty. I've got a class set up
to take care of locking issues on the Queue so one thread can add more
things to the Queue while the other is removing and running a process on the
others. But I can't decide how to signal the process Queue items thread to
run. It makes sense to just signal an event every time an item is added to
the Queue, that will in turn set a flag and start the process Queue. Then,
once the Queue is empty, reset the flag (the flag would be to keep me from
trying to restart the process Queue if it already is running).
This is my first multithreading application, so please understand if this
seems like an obvious answer.
Is there another way of doing this that I'm not thinking of? Here's some
code snippets...
Public Class Broadcaster
Inherits System.ServiceProcess.ServiceBase
Private qThread As Thread
Private WithEvents MyTimer As System.Timers.Timer
Private Q As QWrapper
Shared Sub Main().....
Public Sub New().....
Protected Overrides Sub OnStart(ByVal args() As String)
qThread = New Thread(New ThreadStart(AddressOf QProcess))
MyTimer.Interval = 10000
Mytimer.Enabled = True
End Sub
Private Sub MyTimer(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles MyTimer.Elapsed
Dim reader As Thread = New Thread(New ThreadStart(AddressOf QAdd))
reader.IsBackground = True
reader.Start()
End Sub
Private Sub QAdd()
<...code to add items to the Queue in the QWrapper class...>
End Sub
Private Sub QProcess()
<...code to remove and process items to the Queue in the QWrapper
class...>
End Sub
End Class
class, and run in a loop until that Queue is empty. I've got a class set up
to take care of locking issues on the Queue so one thread can add more
things to the Queue while the other is removing and running a process on the
others. But I can't decide how to signal the process Queue items thread to
run. It makes sense to just signal an event every time an item is added to
the Queue, that will in turn set a flag and start the process Queue. Then,
once the Queue is empty, reset the flag (the flag would be to keep me from
trying to restart the process Queue if it already is running).
This is my first multithreading application, so please understand if this
seems like an obvious answer.
Is there another way of doing this that I'm not thinking of? Here's some
code snippets...
Public Class Broadcaster
Inherits System.ServiceProcess.ServiceBase
Private qThread As Thread
Private WithEvents MyTimer As System.Timers.Timer
Private Q As QWrapper
Shared Sub Main().....
Public Sub New().....
Protected Overrides Sub OnStart(ByVal args() As String)
qThread = New Thread(New ThreadStart(AddressOf QProcess))
MyTimer.Interval = 10000
Mytimer.Enabled = True
End Sub
Private Sub MyTimer(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles MyTimer.Elapsed
Dim reader As Thread = New Thread(New ThreadStart(AddressOf QAdd))
reader.IsBackground = True
reader.Start()
End Sub
Private Sub QAdd()
<...code to add items to the Queue in the QWrapper class...>
End Sub
Private Sub QProcess()
<...code to remove and process items to the Queue in the QWrapper
class...>
End Sub
End Class