1

I have a C# Windows Service that has a Timer that checks every 10 seconds a "flag" in my SQL Table for any Process Pending To Execute.

So right now I have inside my Windows Service:

private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {

    // flag so the process is only executed one at a time.
    if (!_isProcessBusy) {

        RunProcess();

    }
}

private void RunProcessSync() {

    _isProcessBusy = true;

    // ... here all the code for doing the internal process ...


    // when process is Done then change the flag so a new process can be executed
    _isProcessBusy = false;

}

Now I need to let the Windows Service Runs multiple threads of the same process, So if in my SQL Table I have 3 processed to execute then I will let the service run the 3 at the same time.

I was thinking to use System.Threading.Tasks library but don't know if this is the right approach or maybe there is something easier.

svick
  • 236,525
  • 50
  • 385
  • 514
VAAA
  • 14,531
  • 28
  • 130
  • 253

2 Answers2

4

There isn't really a question here but:

Yes. You can use Tasks, and async-await, and Parallel. The simplest option here would probably be to use Parallel.Invoke:

Parallel.Invoke(new Action[] 
{
    () => RunProcess(1), 
    () => RunProcess(2), 
    () => RunProcess(3)
});

This will internally use the TaskParallelLibrary, but it's much simpler. more here: Parallel.Invoke() vs. Explicit Task Management

i3arnon
  • 113,022
  • 33
  • 324
  • 344
  • Ok, I never know how many proceeses there service is executing. What I need is to add into an array each Task when the timer fires and finds a pending process to execute. – VAAA May 14 '14 at 16:37
  • @VAAA that's ok. it was just an example. Parallel.Invoke accepts an array of Actions. Send it an array with as many as you need. – i3arnon May 14 '14 at 16:39
  • So, on my timer_Elapsed function I can add a new Instance of my process to that array right? but do I have to Invoke again? – VAAA May 14 '14 at 16:41
  • @VAAA why add and not just fire how many processes you have waiting using invoke again. – i3arnon May 14 '14 at 16:43
  • ok ok.. going to read more about this becuase its not 100% clear. Thanks a lot – VAAA May 14 '14 at 16:44
  • @VAAA if you actually have an item to to process then maybe TPL Dataflow would be easier for you. Create an ActionBlock, give it a func, set the degree of parallelism and start posting to it. – i3arnon May 14 '14 at 16:47
  • I think the solution is here: http://stackoverflow.com/questions/2293976/how-and-if-to-write-a-single-consumer-queue-using-the-tpl – VAAA May 14 '14 at 16:52
  • @VAAA that's for .net 4.0, before microsoft released TPL Dataflow., I strongly recommend using that instead of an example online. – i3arnon May 14 '14 at 16:56
  • How can I convert the example online to TPL Dataflow? Is that hard? Because I just tried the example and works great – VAAA May 14 '14 at 17:03
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/52696/discussion-between-i3arnon-and-vaaa) – i3arnon May 14 '14 at 17:05
-3

You can create backgroundWorker to make this background processing. (BackgroundWorker is only a "warpper" for thread clases).

Best regards

fabian
  • 6
  • 2