Ok thanks i looked up queue and have this in mind:
1) Object serialised and sent by server
2) Object deserialised client side and resulting object is placed in the
queue.
3) A separate thread would run through the qeueue at all times handling any
elements in it. If the grabbed object is still being handled do not move
onto the next one. I assume some kind of event to be raised after any object
handling is done could be caught here to tell it to move to the next element
in the queue?
As you can see, part 3 is where i have some confusion? Any ideas or
tutorials for a similar situation?
My biggest problem is that my current solution is everytime an object is
received i have a event raised to tell me that item is received. And lots of
listeners for all these events. All being trigegred. So if one object comes
in while another is running i get the sync problem. I need some way of
knowing one is being handled and to fire the next one after.
All help greatly appreciated, this part will be fundamental to my app and i
think will fix a mass amount of bugs from the design flaw.
Normally, queueing between threads works like this:
Thread receives event that an item is received. It deserializes the
item, takes out a lock on the queue, and Enqueues the item (using the
Enqueue method).
Thread polling queue for work wakes up, locks the queue, and tries to
Dequeue an element from the queue, then unlocks the queue. If an item
was successfully dequeued, it processes it and then goes back to lock
the queue and dequeue another item. If it fails to dequeue an item
(there is nothing more to do) then it goes to sleep waiting to be
woken up again.
I believe that there is no need to manually lock / unlock the queue if
you don't use it directly, but rather call Queue.Synchronized to get a
synchronized wrapper for it. I've never done that, myself, so someone
else here will have to say exactly how to do that.
So, you see, there's no need to mark items in the queue as "being
processed". You grab the item _off_ the queue and _then_ process it.
Everything in the queue is waiting to be processed, so producing
threads just have to enqueue items and then kick the consuming thread
to wake it up (if it's not awake already).
The only tricky bit is how to wake up the processing thread (or
threads, as you may scale this to have more than one thread processing
elements from the queue). I believe that Jon Skeet's pages on
multithreading contain a standard producer / consumer example for
multithreading. Take a look here:
http://www.yoda.arachsys.com/csharp/threads/