while and sleep?

 

Hi all,

A friend of mine asked me a question, I simplified it down to the following test code:

extern int       TimeInterval=900;    // xx minutes to send the e-mail

int start()
  {
   while(TimeInterval>0)
   {   
       SendMail("someTITLE","someTEXT" );
       Sleep( TimeInterval*60*1000);    //sleep in miliseconds, so use 60*1000 to change to minute
   }
  }

By dragging this script to chart, it sent only one email, that's fine. Then, if I change current chart's timeframe( I assume it's the same when closing MT4), when the script exited, it sent emails again and again, every 2 seconds or so.

I also tried replacing sendmail with a simple counter and print it out, same thing happened.

Why ? Could anybody please help?

 
   while(TimeInterval>0)
   {   
       SendMail("someTITLE","someTEXT" );
       Sleep( 60*1000);    //sleep in miliseconds, so use 60*1000 to change to minute
       TimeInterval--;
   }



 
joshatt:

Hi all,

A friend of mine asked me a question, I simplified it down to the following test code:

By dragging this script to chart, it sent only one email, that's fine. Then, if I change current chart's timeframe( I assume it's the same when closing MT4), when the script exited, it sent emails again and again, every 2 seconds or so.

I also tried replacing sendmail with a simple counter and print it out, same thing happened.

Why ? Could anybody please help?

If you only want it to send one email then you don't need the While statement just sleep, send, exit...

Your loop will continue to send email every interval forever if the interval is > 0

When you change timeframe on MT4 with a script running is asks you if you really want  to stop running the script.

extern int       TimeInterval=900;    // xx minutes to send the e-mail

int start()
{
   Sleep( TimeInterval*60*1000);    //sleep in miliseconds, so use 60*1000 to change to minute
   SendMail("someTITLE","someTEXT" );
}
 
Paul Anscombe:

If you only want it to send one email then you don't need the While statement just sleep, send, exit...

Your loop will continue to send email every interval forever if the interval is > 0

When you change timeframe on MT4 with a script running is asks you if you really want  to stop running the script.

Thanks for your help.

I want it repeated.

And I don't know it's stopping script or starting script on new timeframe that's causing this weird problem - sleep/interval failed to work.

 
joshatt:

And I don't know it's stopping script or starting script on new timeframe that's causing this weird problem - sleep/interval failed to work.

Is this really a script or is it an indicator?

Sleep() does not work in indicators.

 
Keith Watford:

Is this really a script or is it an indicator?

Sleep() does not work in indicators.

script.

 
joshatt:

script.

I was not aware of this, but it seems that when you exit a script, the loop will continue until it completes but the Sleep() will be ignored.

joshatt:

Then, if I change current chart's timeframe( I assume it's the same when closing MT4), when the script exited, it sent emails again and again, every 2 seconds or so.

As your code has an endless loop, it will go on forever. You probably only get an email every 2 seconds because 1000's of emails in rapid succession will not be allowed.

Did you take note of my reply #1 ?

You should get into the habit of using the up to date functions and always use property strict.

All you have to do is click on "New", top left of the compiler, select what you want to code, in this case a script and voila you have the basic frame.

//+------------------------------------------------------------------+
//|                                                          qqq.mq4 |
//|                                                    Keith Watford |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Keith Watford"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   
  }
//+------------------------------------------------------------------+

Same for indicators and EAs.


Personally I don't tend to use Sleep() in scripts as I have not really found any reason to.

For code along the lines of yours, I would use an indicator and OnTimer().

 
Keith Watford:

I was not aware of this, but it seems that when you exit a script, the loop will continue until it completes but the Sleep() will be ignored.

As your code has an endless loop, it will go on forever. You probably only get an email every 2 seconds because 1000's of emails in rapid succession will not be allowed.

Did you take note of my reply #1 ?

You should get into the habit of using the up to date functions and always use property strict.

All you have to do is click on "New", top left of the compiler, select what you want to code, in this case a script and voila you have the basic frame.

Same for indicators and EAs.


Personally I don't tend to use Sleep() in scripts as I have not really found any reason to.

For code along the lines of yours, I would use an indicator and OnTimer().

Thank you, I appreciate it!

I'll remember this. 

Reason: