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

 
Sleep(0);

Such a slip can take tens of milliseconds.

 
fxsaber:

Such a slip can run for tens of milliseconds.

What does it mean?

I remember a long time ago Sleep() was mentioned in paralleling processes

 
Fast528:

What does that mean?

Exactly what he said.

 
fxsaber:

Such a slip can run for tens of milliseconds.

Not tens, but 15.625 milliseconds.
I have been using the construction for a long time:

if(pause>0) Sleep(pause);
 
Nikolai Semko:

Not tens, but 15.625 milliseconds.
I have been using the design for a long time:

ulong GetSleepTime( const int Pause = 0 )
{
  const ulong StartTime = GetMicrosecondCount();
  
  Sleep(Pause);
  
  return(GetMicrosecondCount() - StartTime);
}

void OnStart()
{
  ulong MaxTime = 0;
  
  while (!IsStopped())
  {
    const ulong SleepTime = GetSleepTime();
    
    if (SleepTime > MaxTime)
    {
      Print(SleepTime);
      
      MaxTime = SleepTime;
    }
  }
}


Result

2542
9983
10033
10041
10148
10348
11982
26116
27529
40066
49915
56319
 
fxsaber:


Result

Calculate the average time.

Accumulate SleepTime, and divide it by the number of iterations after the loop ends.

Otherwise, the presented result looks scary to immature minds. Show me the minimum time while you're at it.

PS. The Sleep() function in MQL5 is not a redirect to the win api function ::Sleep(). Well, if the value is less than 100 it's a redirect. But above 100 it is a loop with win api slip inside, so that it can be interrupted by IsStopped.

And there is one more nuance. At value of milliseconds <=0 we substitute 1. That is, we never call ::Sleep(0).

PPS import the Sleep function from kernel32.dll and do the same experiment with it

 
Slava:

Calculate the average time.

Accumulate SleepTime, and divide it by the number of iterations after the cycle ends.

I was not talking about the average time, but about the possibility of spikes. I encountered them when waiting for the trading history to be synchronized with the OrderSend result. I was waiting for it through Sleep(0). It turned out that it cannot be done that way.

Slava:

PS. The Sleep() function in MQL5 is not a redirect to the win api function ::Sleep(). Well, if the value is less than 100, this is a redirect. But above 100 it is a loop with win api slip inside, so that it can be interrupted by IsStopped.

And there is one more nuance. At value of milliseconds <=0 we substitute 1. That is, we never call ::Sleep(0).

Thank you for the details. I didn't know that Sleep(5000) can be terminated through IsStopped().

 
fxsaber:

It was not about the average time, but about the possibility of spikes. I encountered them while waiting for the trading history to be synchronized with the OrderSend result. I was waiting via Sleep(0). It turned out that it cannot be done that way.

So the Windows operating system has never been a real-time system
 

fxsaber:

Thanks for the details. I didn't know that Sleep(5000) can be broken via IsStopped().

https://www.mql5.com/ru/docs/common/sleep

The function has a built-in check for the status of the Expert Advisor stop flag every 0.1 second.

Документация по MQL5: Общие функции / Sleep
Документация по MQL5: Общие функции / Sleep
  • www.mql5.com
Функцию Sleep() нельзя вызывать из пользовательских индикаторов, так как индикаторы выполняются в интерфейсном потоке и не должны его тормозить. В функцию встроена проверка состояния флага остановки эксперта каждую 0.1 секунды.
 
Slava:
So the Windows operating system has never been a real-time system

To be honest, I don't even know what it means and where in MQL5 you can encounter it.