Features of the mql5 language, subtleties and tricks - page 187

 

Has anyone come across a multi-timer solution so that I don't have to reinvent the wheel myself?
I don't see the implementation as being complicated by using pointers to the function. It is unlikely that it would not have already been implemented before me.
That is, the original task is to create a timer with any periodicity and assign to it a function to handle the timer event. And there can be any number of timers.

Like this:

#include <Timer.mqh> 

CTimer timers;
int x1=0;
int x2=0;

int OnInit()
  {
   timers.NewTimer(500, Sum1);  // создаем новый таймер с периодом обновления 500  милисекунд и функцией-обраточником Sum1()
   timers.NewTimer(2000, Sum2); // создаем новый таймер с периодом обновления 2000 милисекунд и функцией-обраточником Sum2()
   return(INIT_SUCCEEDED);
  }

...

void OnTimer()
  {
    timers.OnTimer();   
  }

//+------------------------------------------------------------------+

void Sum1() { // данная функция вызывает с периодичностью 500 милисекунд
x1++;
if (x1==10) timers.KillTimer(Sum1); // удаляем этот таймер 
}

void Sum2() { // данная функция вызывает с периодичностью 2000 милисекунд
x2++;
if (x2==10) timers.KillTimer(Sum2); // удаляем этот таймер
}
 
Nikolai Semko:

No one has come across a multi-timer solution

I'm sure I've seen it at the KB a long time ago.
 
fxsaber:
I'm sure I've seen it at the Buy More a long time ago.

I can't find it. Strange.
OK, I'll implement it myself. Shouldn't be any pitfalls. I'll post it at the Buy More.

 
Nikolai Semko:

I.e. the initial task is to create a timer with any periodicity and map its timer event handling function to it. And there can be any number of timers.

simpler variant won't do?

input int            OnTimerMs               = 100;
int OnInit()
{
   EventSetMillisecondTimer(OnTimerMs);
   return(INIT_SUCCEEDED);
}
void  OnTimer()
{
   static uint tickcount_500ms = 0;
   static uint tickcount_1s = 0;
   static uint tickcount_1M = 0;
   uint gettickcount = GetTickCount();
   bool event_500ms  =  gettickcount - tickcount_500ms >= 500;
   bool event_1s     =  gettickcount - tickcount_1s >= 1000;
   bool event_1M     =  gettickcount - tickcount_1M >= 60000;
   

// счетчики 500мс, 1с и 1М
   if(event_500ms)
   {
      tickcount_500ms = gettickcount;
.....
   }

   if(event_1s)
   {
      tickcount_1s = gettickcount;
.....
   }

   if(event_1M)
   {
      tickcount_1M = gettickcount;
......
   }

}

UPD: And so, you can use fxsaber code as a template

here's how to make it so that all objects are automatically deleted https://www.mql5.com/ru/forum/325418/page4#comment_16116740

 
Nikolai Semko:

I can't find it. Weird.
OK, I'll implement it myself. Shouldn't be any pitfalls. I'll put it in the Buy More.

done
https://www.mql5.com/ru/code/31306

Multi Timer
Multi Timer
  • www.mql5.com
1. Включите в свою программу класс CTimer 2. Создайте один объект класса СTimer. Например timers. 3. Создайте функции-обработчики необходимых таймеров формата void AnyNameFunction() {....} например с такими именами: 4. Когда вам необходимо создать и запустить таймер, сделайте так из любого места: 5. Когда вам таймер больше не нужен...
 
Igor Makanu:

a simpler version wouldn't work?

UPD: otherwise, you can use fxsaber code as a template

here's how to make it so that all objects are automatically deleted https://www.mql5.com/ru/forum/325418/page4#comment_16116740

Of course, you could do it that way. But less readable and more difficult to manage.

 

Once upon a time (roughly) did this.

It's inconvenient to use with classes.

class CWithTimer {

public:

    void CallMeByTimer();

};

And external (to the class) timer with pointers to the function is powerless.

 
Maxim Kuznetsov:

once upon a time (roughly) did so.

It's inconvenient to use with classes.

class CWithTimer {

public:

    void CallMeByTimer();

};

And external (to the class) timer with pointers to the function is powerless.

If you mean function as a class method, then yes, I agree. I realised this myself yesterday and am puzzled as to how to work it out.
I would like to use this class inside another class, otherwise the use is very limited.
At least a singleton must still be implemented and the timer already running outside the class must be controlled.
 
Nikolai Semko:
If you mean function as a class method, then yes, I agree. Figured it out myself yesterday and am puzzling over how to get out of it.
I would like to apply this class inside another class, otherwise the application is very limited.
At the very least, we need to implement a singleton and control the timer already running outside the class.
There should be a pointer to the class with the timer in the object.
 
Vladimir Simakov:
The object must contain a pointer to a class with a timer.
The question is how to run a non-static method of the class from the pointer, even if the object of another class (in this case CTimer) is a property (variable) of that class. Is it possible. I'm afraid not.
Reason: