• 0

[C#]Server Design


Question

I'm trying to create a processing component that simply has a queue of objects that it has to process. I'm trying to create a worker thread that simply processes the queue as long as something is in it and pauses or sleeps until something else is placed in the queue. The problem I'm having is finding the best way of doing it. I've thought about having the thread sleep and check occationally if something has been added, but that just seems like a waste. I've looked at Thread.Suspend and Thread.Resume, but MSDN suggests that I should use Monitor.Wait for this type of activity because Suspend and Resume shouldn't be used in this matter but I feel that it's far too complicated for this simple issue. I think I'm just missing something. Somebody out there has had to have done something like this before. Any help?

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

When data gets added to the que, wake up the worker thread? I am not sure about how you are adding to the queue, the keeping the model event driven will eleminate the need for polling.

Link to comment
Share on other sites

  • 0

Why don't you simply use your app main thread to look for something in the queue, and spawn a worker thread when it finds it non-empty (might be done using windows messages, like KayMan2K suggested). The worker thread could either do all the queue itself until it finds it empty, then destroy itself, or it could spawn other worker threads if you want to do parallel processing.

Your main thread would only spawn a worker thread when the queue is non-empty and no worker thread(s) are at work.

Link to comment
Share on other sites

  • 0

Create a (collection?) class for your queue, and when something is added to it, it resumes/awakens the thread. Or just wake it up manually each time you add something to the queue, but depending on your application design, this may not be an option, hence my first suggestion...

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.