Good day.

I have a windows-service that performs 5 tasks: 4 of them are executed in 1 minute, the 5th - in 15 minutes. The service has a timer that runs these tasks for every second.

Code:

class SmsInspectorTasks { DbHelper _dbHelper; List<Event> _list; LoggerHelper _loggerHelper; Settings _settings; /* * Task1 - Работа с балансами * Task2 - Работа с статусами * Task3 - Проверка на неотправленые сообщения * Task4 - Отправка ежемесячных затрат по Клиентам * Task5 - Обработка массовой рассылки */ public SmsInspectorTasks(Settings settings, LoggerHelper loggerHelper) { _dbHelper = new DbHelper(settings.DBConnection, settings.LogsPath); _list = _dbHelper.GetEvents(); _loggerHelper = loggerHelper; _settings = settings; _loggerHelper.WriteLog("*** SmsInspectorTasks конструктор", true); } #region Работа с балансами Task Balances() { Task T = new Task(GetBalances); T.Start(); return T; } void GetBalances() { ///.... } #endregion #region Работа с статусами Task Statuses() { Task T = new Task(GetStatuses); T.Start(); return T; } void GetStatuses() { //... } #endregion #region Проверка на неотправленые сообщения Task NonSendMessages() { Task T = new Task(GetNonSendMessages); T.Start(); return T; } void GetNonSendMessages() { //... } #endregion #region Отправка ежемесячных затрат по Клиентам Task ClientCosts() { Task T = new Task(GetClientCosts); T.Start(); return T; } void GetClientCosts() { //... } #endregion #region Обработка массовой рассылки Task Bulks() { Task T = new Task(GetBulks); T.Start(); return T; } void GetBulks() { //... } #endregion /// <summary> /// Запустить все задачи на выполнение /// </summary> public void StartAll() { var balanceTask = Balances(); var statuseTask = Statuses(); var nonSendMsgTask = NonSendMessages(); var costs = ClientCosts(); var bulks = Bulks(); } } 

Roughly speaking, there are 5 Task objects.

That’s not working - after having completed procedure No. 5 in 15 minutes, the service just hangs and does nothing.

Tell me, please, how to make the service work normally every second, and task number 5 was carried out quietly for 15 minutes (it calls once a month !!!)

As I understand it, you can't do Task.WaitAll () because the service will then stand idle for 15 minutes.

I think to make a new timer (15 min interval) only for this task.

How would you do in my place?

Thank.

UPD

Timer Code:

 using Timer = System.Timers.Timer; 

...

  _timer.Elapsed += _timer_Elapsed; _timer.Interval = 1000; _timer.Enabled = true; 

...

  private static void _timerDogwatch_Elapsed(object source, ElapsedEventArgs e) { SmsInspectorTaskssi = new SmsInspectorTasks(); si.StartAll(); } 
  • one
    Too much code, keep the example to a minimum - ixSci
  • Remove from the example of working with a database, XML and all that. They are definitely not relevant to this issue. - VladD
  • Thank you, corrected - Leonard Bertone
  • @LeonardBertone: DbHelper _dbHelper is still there :) - VladD
  • one
    Show the timer code. - Dmitry Dovgopoly

1 answer 1

You probably forgot to set the AutoReset property for the timer - so it works only 1 time.


Tip: create a separate timer for each task - the easiest way.

By the way, if you assign a null to the SynchronizingObject property of the timer, then you will not need to create a new thread for the task, the timer will create it yourself. Useless duplicate pieces of code will go away.