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

 
Andrey Khatimlianskii:

No one has guaranteed that other event handlers will be interrupted (or not run).

After the alert, both deinit and init happen, right?

Yes, there is a complete reboot going on. It's just the timer (even a second one) that gets pounced on - a feature, not a bug.

 
fxsaber:

This advisor alerts when switching between accounts. It would seem that it should not do this, according to the rule

However, this happens because of the timer.

This peculiarity can be used to solve a simple in formulation, but complex in implementation.

// Если произошла смена счета, советник выгружается.

const bool Init = EventSetMillisecondTimer(1);

void OnTimer()
{
  static const long Account = AccountInfoInteger(ACCOUNT_LOGIN);
  
  if (Account != AccountInfoInteger(ACCOUNT_LOGIN))
    ExpertRemove();
}
 
fxsaber:

This feature can be used to solve a simple in formulation but complex in implementation.

Why can't we just check the account change at the input of each On-function?

Why a millisecond timer? It won't interrupt program execution anyway if a re-login occurs during the runtime.

 
Andrey Khatimlianskii:

Why can't you just check the account change at the input of each On-function?

Why a millisecond timer? It won't interrupt program execution anyway if a re-login happens while it's running.

Try it. There is a long discussion on this topic at the link above.

 
fxsaber:

Yes, there is a complete reboot going on. It's just the timer (even a second timer) that gets bumped up - a feature, not a bug.

After the ExpertRemove() command the stop flag is simply raised and the EA continues to work until the next tick. On a new tick, the EA will be unloaded when the stop flag is raised. It means that until a new tick comes, all On-functions continue to work in the normal mode.

Accordingly, if we can't get the state of this flag programmatically (I haven't specifically looked for this feature), then we need to have a globally custom flag. We set our own flag before the ExpertRemove() command and check it in each On-function. If the flag is set, we will leave. Then the timer will not execute code that is written in it.

Or maybe I've got it wrong and the question is different?

 
Artyom Trishkin:

After the ExpertRemove() command, the stop flag is simply set and the EA continues to work until the next tick. If the stop flag is raised on a new tick, the EA is unloaded. It means that until a new tick comes, all On-functions continue to work in the normal mode.

Accordingly, if we can't get the state of this flag programmatically (I haven't specifically looked for this feature), then we need to have a globally custom flag. We set our own flag before the ExpertRemove() command and check it in each On-function. If the flag is set, we leave. Then the timer will not execute code that is written in it.

Or maybe I've got it all wrong and the question is different?

I don't know what problem you're describing.

 
Artyom Trishkin:

After the ExpertRemove() command, the stop flag is simply set and the EA continues to work until the next tick. If the stop flag is raised on a new tick, the EA is unloaded. It means that until a new tick comes, all On-functions continue to work in the normal mode.

Accordingly, if we can't get the state of this flag programmatically (I haven't specifically looked for this feature), then we need to have a globally custom flag. We set our own flag before the ExpertRemove() command and check it in each On-function. If the flag is set, we leave. Then the timer will not execute code that is written in it.

Or maybe I've got it all wrong and the question is different?

Not on a new tick, but on the next return;

Put ExpertRemove() in OnInit() and check it in the debug step by step.

 
Alexey Viktorov:

Not on a new tick, but on the next return;

Put ExpertRemove() in OnInit() and check it step by step in debug.

Yes, my mistake. Any next event will not be executed. It's a long time since I've looked in help for functions I don't need :)

 
fxsaber:

I don't know what problem you are describing.

The meaning of the answer is not clear. It doesn't matter.

 
Artyom Trishkin:

The meaning of the answer is not clear.

ExpertRemove was not invoked.