0

I have a windows service which performs multiple task which i have separated into functions, some will take lets say 5 minutes to complete, while some will take less.

private System.Timers.Timer tim = null;
 protected override void OnStart(string[] args)
    {

        tim = new System.Timers.Timer();
        this.tim.Interval = 30000;
        this.tim.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimedEvent_Tick);
        tim.Enabled = true;

    }
private void OnTimedEvent_Tick(Object source, System.Timers.ElapsedEventArgs e)
 {
            Task task0 = Task.Factory.StartNew(() => Function1()); // doing some database operations
            Task task1 = Task.Factory.StartNew(() => Function2()); // doing some other database operation
            Task task10 ......Up to Function10()
            Task.WaitAll(task0,task1, task2, task3, task4,task5, task6,task7,task8,task9,task10);

 }

Is there a draw back to the above method? if my windows service is to run lets say every 30 seconds. IF there is how do i approach it?

Dexter
  • 93
  • 9

3 Answers3

0

This would work fine in your case since there are only limited number of tasks. For cases where number of tasks that can be created are unknown, do consider using Parallel.ForEach instead. The thing that you need to handle here is exception. Put your code in a try.... catch statement.

    try
            {
    ....your code here..... 
}
    catch (AggregateException e)
            {
    var x = e.Flatten();
    // Log or do what-ever
    }
Amit Kumar Singh
  • 4,393
  • 2
  • 9
  • 22
  • thank you for your reply, what if Function1/2/3 etc has not completed and 30 seconds timer runs out ? – Dexter Aug 09 '17 at 16:15
  • What you are calling here is a delegate. Inside it you are creating tasks. So, timer has really nothing to do once it starts the delegate. After its interval is done, it will fire again in another thread. A way to handle it will be that you can disable timer after first call. Thereafter, run an infinite while loop and your code inside it. At the last line of your code, you can write Thread.Sleep. https://stackoverflow.com/questions/19151363/windows-service-to-run-a-function-at-specified-time https://stackoverflow.com/questions/18001733/net-multiple-timers-instances-mean-multiple-threads – Amit Kumar Singh Aug 09 '17 at 16:27
0

The correct answer would depend on what those tasks are actually doing. If all tasks must be completed prior to restarting any of them, set tim.AutoReset = false. Then after Task.WaitAll() call tim.Start(). This will ensure your wait time is between complete executions of all tasks. Otherwise, if your timer time is smaller than task execution time, you won't see any wait time.

ScottTx
  • 1,483
  • 8
  • 12
0

If some of your functions will periodically take longer than timer interval (30 seconds) it will cause threads count to increase without any control. So you will end by using all possible threads which will result in processing delays. If timer interval is shorter than processing time, consider applying pause-resume timer system

Tomas Chabada
  • 2,869
  • 1
  • 17
  • 18