C
Craig Buchanan
I would like to have a limited pool of objects (the objects are expensive to
create) that can be enlisted to process items in a queue. Moreover, I
would like to be able to have the processing (time-consuming) occur on a
background thread.
I have two questions:
* what is a good way to block the queue loop while it waits for a free
worker?
* is my approach a reasonable one?
Thanks,
Craig Buchanan
Here's my psedo-code:
Sub ProcessQueue
Do While Queue.Count > 0
'wait for next available worker
Worker = ObjectPool.getFreeWorker()
'assign next item in the queue to the worker
Worker.Item = Queue.Dequeue
'run time-consuming process in background
ThreadPool.QueueUserWorkItem(AddressOf ProcessItem, Worker)
Loop
End Sub
Sub ProcessItem(State As Object)
Dim WorkerAs Worker= CType(State,Worker)
Try
'process item
Worker.Process()
Catch ex As Exception
'return item to queue
Queue.Enqueue(Worker.Item)
Finally
'return worker to pool
ObjectPool.returnWorker(Worker)
End Try
End Sub
create) that can be enlisted to process items in a queue. Moreover, I
would like to be able to have the processing (time-consuming) occur on a
background thread.
I have two questions:
* what is a good way to block the queue loop while it waits for a free
worker?
* is my approach a reasonable one?
Thanks,
Craig Buchanan
Here's my psedo-code:
Sub ProcessQueue
Do While Queue.Count > 0
'wait for next available worker
Worker = ObjectPool.getFreeWorker()
'assign next item in the queue to the worker
Worker.Item = Queue.Dequeue
'run time-consuming process in background
ThreadPool.QueueUserWorkItem(AddressOf ProcessItem, Worker)
Loop
End Sub
Sub ProcessItem(State As Object)
Dim WorkerAs Worker= CType(State,Worker)
Try
'process item
Worker.Process()
Catch ex As Exception
'return item to queue
Queue.Enqueue(Worker.Item)
Finally
'return worker to pool
ObjectPool.returnWorker(Worker)
End Try
End Sub