How do I service a single class on one low priority thread?

E

Erik

Hi Everyone,

I was thinking of how to do this for a while now and I cant get my
head round it so I though I'd ask the experts! :)

What I am doing is reading in large amounts of data over TCP and
saving it to disk. I receive alot of data from alot of clients so I do
all this without processing the data.

When a piece of data from a client has been completed I want to do
some processing on it and then send it off to another server. I want
to do all this in a low priority thread so as not to interfere with
the main program. What I would like to do is just add the job to a
queue and have a thread waiting ready to service the queue when a job
arrives. I may have 100 items on the queue or only 1. I just want this
thread to trundle away in the background pulling a job off the queue
processing it and then getting another job etc. If the queue is empty
then I would expect it to just sit there idle until an item is added.
Is this possible? How would I go about it or is there a better way?

Many thanks
 
J

Jon Skeet [C# MVP]

Erik said:
I was thinking of how to do this for a while now and I cant get my
head round it so I though I'd ask the experts! :)

What I am doing is reading in large amounts of data over TCP and
saving it to disk. I receive alot of data from alot of clients so I do
all this without processing the data.

When a piece of data from a client has been completed I want to do
some processing on it and then send it off to another server. I want
to do all this in a low priority thread so as not to interfere with
the main program. What I would like to do is just add the job to a
queue and have a thread waiting ready to service the queue when a job
arrives. I may have 100 items on the queue or only 1. I just want this
thread to trundle away in the background pulling a job off the queue
processing it and then getting another job etc. If the queue is empty
then I would expect it to just sit there idle until an item is added.
Is this possible? How would I go about it or is there a better way?

Well, see the link below for a producer/consumer queue. Then just
create a thread which is going to be the consumer, set it to be low
priority, and start it.

http://www.pobox.com/~skeet/csharp/threads/deadlocks.shtml (half way
down the page)
 
W

William Stacey [MVP]

Also remember that thread priority is system wide, not just application
wide. So using a lower priority may actually negatively effect your
performance. If your consumer keeps falling too behind because it is on a
lower priority, then at some point your producer(s) will out run your
consumer and either fill the queue (if bounded) or fill all memory. If your
writing to disk, this may be less of a concern. Here is an UI example I did
using my Bounded Blocking queue on Channel9:
http://channel9.msdn.com/ShowPost.aspx?PostID=50960
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top