Error: cannot set timer (1)

 
Good morning.

You're giving the following error (cannot set timer (1)) when I add my indicator to the chart.

After some internet searches I found the code below as a possible solution, but it didn't work.

//--- Create timer
int error   = -1;
int counter = 1;
do
  {
   ResetLastError();
   EventSetTimer(1);
   error = GetLastError();
   Print("EventSetTimer(delay). Attempt = ",counter," Error = ",error);

   if(error != 0)
      Sleep(1000);
   counter++;
  }
while(error != 0 && !IsStopped() && counter <= 100);

Does anyone know what I can do?

Note: I added the above code to the OnInit() function.

Thank you.

 
what timer, countdown?
 
MikelDavao:
what timer, countdown?

There is a loop that counts down to show the history of incoming.

 

You only have to set it once in OnInit() function and not over and over in a do while loop.

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create a timer with a 1 second period
   EventSetTimer(1);
 
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
 
  1. Marco vd Heijden: You only have to set it once in OnInit() function and not over and over in a do while loop.

    You have to on MT4, because sometime it returns an error (4030.)

    EST can fail. The default suggestion is to loop until you enable it.
              Build 1080 Cannot Set Timer error with EventSetTimer function - Indices - MQL4 programming forum #8

    I don't try to enable them in OnInit but instead I keep trying to enable them in OnTick/OnCalculate until they succeed. (No race condition, no CPU loop, no timer ticks before chart is updated.)
              Cannot set timer (60) - EA not working - MT4 - MQL4 programming forum

    int OnInit(){
       while(!EventSetTimer(1) ){
          alert_id(StringFormat("EST:%i", _LastError) );
          Sleep(5000);   if(_StopFlag) return INIT_FAILED;
       }

    1  09:31:41.071   order_graphic_tool_v6 USDCHF,Daily: cannot set timer (1)
    0  09:31:41.071   order_graphic_tool_v6 USDCHF,Daily: Alert: order_graphic_tool_v6 USDCHF D1 EST:4030
    0  09:31:41.071   order_graphic_tool_v6 NZDCAD,Daily: Alert: order_graphic_tool_v6 NZDCAD D1 EST:4030
    1  09:31:41.071   order_graphic_tool_v6 GBPCHF,Daily: cannot set timer (1)
    1  09:31:41.071   order_graphic_tool_v6 AUDSGD,Daily: cannot set timer (1)
    0  09:31:41.071   order_graphic_tool_v6 GBPCHF,Daily: Alert: order_graphic_tool_v6 GBPCHF D1 EST:4030
    0  09:31:41.071   order_graphic_tool_v6 AUDSGD,Daily: Alert: order_graphic_tool_v6 AUDSGD D1 EST:4030
    1  09:31:41.071   order_graphic_tool_v6 USDCAD,Daily: cannot set timer (1)
    1  09:31:41.071   order_graphic_tool_v6 GBPJPY,Daily: cannot set timer (1)
    0  09:31:41.071   order_graphic_tool_v6 GBPJPY,Daily: Alert: order_graphic_tool_v6 GBPJPY D1 EST:4030
    0  09:31:41.071   order_graphic_tool_v6 USDCAD,Daily: Alert: order_graphic_tool_v6 USDCAD D1 EST:4030

    It's some type of race condition, highly variable.
    Jeovane Reges: I added the above code to the OnInit() function.
    In EA I'm using 5 second sleep, which usually doesn't result in a second attempt. For indicators try and set it in OnCalculate. On failure return zero and retry next tick. This drastically reduces the problem.
 
JOSE JEOVANE REGES CORDEIRO Reges:

There is a loop that counts down to show the history of incoming.

I had the same problem. If indicator use DLL go to indicator->common tab and enable "allow dll import"

It solved my problem..

 
I was getting this on a VPS I have which has only 1 CPU. On a 2 CPU VPS I do not see the error. Also, I was only getting it when I hand more than 2 charts running which were setting the timer. It was random whereby sometimes I would get the error and sometimes not, sometimes on 1 chart and sometimes on all of them. Only happened when starting the MetaTrader4 platform, if I load the EA manually for each chart after MetaTrader4 has started then I did not get the error.
 

What are you doing?   I dont understand.

It should be very easy.  All you need are two built in functions.

int OnInit()
  {
       //--- create timer
       EventSetTimer(1);    // Timer set to 1 second
       return(INIT_FAILED);

  }

void OnTimer()             //  This function runs every time the Event timer goes off.
  { 

     // do something here every second

  }
 

There can be set only one timer per script – so maybe your indicator is using some other timers? For example within the used script or other indicator?
But as others have mentioned – you should place EventSetTimer on the OnInit and I recommend also EventKillTimer added to OnDeinit function (there is no OnDeinit initially on the indicators schema but you can add it) or you can try first kill a timer and then restart it like that (I set it up to 10 attempts to not block the whole indicator):

void OnInit(){

                bool set = EventSetTimer(1);

for(int i=0; i<10; i++){

   EventKillTimer();

   set = EventSetTimer(1);

}

}

 
Marzena Maria Szmit #: There can be set only one timer per script – so maybe your indicator is using some other timers? For example within the used script or other indicator? But as others have mentioned – you should place EventSetTimer on the OnInit and I recommend also EventKillTimer added to OnDeinit function (there is no OnDeinit initially on the indicators schema but you can add it) or you can try first kill a timer and then restart it like that (I set it up to 10 attempts to not block the whole indicator): 

Please edit your post and use "</>" or Alt-S to properly insert your code. Don't just use plain text. It becomes unreadable.

 
Chris Pinter #: What are you doing?   I dont understand. It should be very easy. 

I should be, but it's not. What part of “sometime it returns an error (4030),” and “it's some type of race condition, highly variable,” was unclear?

Reason: