0

This may not be suitable here, please feel free to move, shout or abuse if so.

We currently have a console application that get started by another and passed in an ID of the 'job', this job will have multiple records that need to be processed. A simple explanation of the flow would be;

Starts 50 threads

Gets records to be processed.
if records > 0 see what threads are not still busy and send it some information.
if records = 0 update something else and exit.
Get more records.
Loop.

Now, I am looking to convert this into a 'polling' service that is continually running and when new records are available, process them. To take what I have and convert this is fairly simple, but the threads stuff is old and probably outdated.

I was looking to refactor most if not all and use Task.Parallel to process the items. However, I am struggling to get a suitable framework for polling and then processing the items and was looking for suggestions on how to achieve this.

Pretty vague I know, but hopefully enough to give some kind of input.

Many thanks

svick
  • 236,525
  • 50
  • 385
  • 514
ChrisBint
  • 12,773
  • 6
  • 40
  • 62

3 Answers3

1

From my experience and this msdn quote:

More efficient and more scalable use of system resources.

Behind the scenes, tasks are queued to the ThreadPool, which has been enhanced with algorithms (like hill-climbing) that determine and adjust to the number of threads that maximizes throughput. This makes tasks relatively lightweight, and you can create many of them to enable fine-grained parallelism. To complement this, widely-known work-stealing algorithms are employed to provide load-balancing.

You simply shouldn't care about how many tasks is a good number, or how to create a system where you load balance the threading involved.

Simply use:

Task.Factory.StartNew(() => DoSomeWork());

Every time you want to run something asynchronously, it does all the smart job behind the curtain.

Now since you're likely to create tasks in a loop, please be extra-careful not to introduce a closure bug many people had (including me), which you can look up here.

I have a windows service that runs from 1 to 500 Tasks, and never had trouble.

Hope this helps,

Bab.

Community
  • 1
  • 1
Louis Kottmann
  • 16,268
  • 4
  • 64
  • 88
1

If you are polling for new records in a DB table, a better approach would be to install an INSERT-trigger (and possibly also UPDATE- and DELETE-triggers) on this table and to send a message to your service when a new records is inserted.

See Posting Message to MSMQ from SQL Server on MSDN.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

The "polling service" sounds like a nice case for an observable collection. There's Rx, a nice way to handle them (http://rxwiki.wikidot.com/101samples), which I think uses the TPL.

phipsgabler
  • 20,535
  • 4
  • 40
  • 60