How to Remove a service - page 2

 
Dominik Christian Egert #:
Thank you for the article.

Although docs state ExpertRemov() is prohibited in services, it is actually not and doing exactly as expected. Calling it will set the flag _StopFlag and IsStopped() will be true from that point in time on.


So, maybe that's either a docs-issue or an Implementation bug.

Because it does do the job, except for removing the service.

I only had a cursory look at it, but from it I got the impression that a service terminates much like a script:

Let's expand the capabilities of the previous service and name the new one dStartStop.mq5.

//+------------------------------------------------------------------+
//| Service program start function                                   |
//+------------------------------------------------------------------+
void OnStart()
  {
   string program_name=::MQLInfoString(MQL_PROGRAM_NAME);
   datetime now=::TimeLocal();
   ::PrintFormat("Service \"%s\" starts at: %s", program_name,::TimeToString(now, TIME_DATE|TIME_SECONDS));
   ::Sleep(1000);
   now=::TimeLocal();
   ::PrintFormat("Service \"%s\" stops at: %s", program_name,::TimeToString(now, TIME_DATE|TIME_SECONDS));
  }
//+------------------------------------------------------------------+
 
Carl Schreiber #:

I only had a cursory look at it, but from it I got the impression that a service terminates much like a script:

Yap, it does. But a script will terminate and unload, it will not run again, if the terminal is restarted, contrary to a service, once it is added, it will autorun every time you start the terminal.

While an EA, when terminated by ExpertRemove, it won't restart on Terminal startup.





 
Dominik Christian Egert #:
haha....

Not funny. 😀

Critical error was relating to something immutable from the service, like a missing "Allowed URL" or a repeated failure, not recoverable from within the service. iE a missing CSV file or similar. Or maybe a malformed link from input, where you get repeated error by WebRequest () function.



So, that's what I was looking at.

My conclusion was to have the service being removed, such that the user needs to interact, and the service doesn't autorun each time I start the terminal.

Maybe you could ask a dev to include the removal of services with ExpertRemove... 😉

You can check for that conditions and warn the user to fix it. The service can still work until it's fixed. No need to remove it.

You would need to convince me it's needed, then I could ask.

EDIT: Just to be clear I don't have any power, just good reputation and relations so I can ask when I am convinced something is a good idea.

 
Dominik Christian Egert #:
Yap, it does. But a script will terminate and unload, it will not run again, if the terminal is restarted, contrary to a service, once it is added, it will autorun every time you start the terminal.

While an EA, when terminated by ExpertRemove, it won't restart on Terminal startup.

Well this (autostart on start of the terminal) what a service makes a service and it was offered exactly for this. Is you don't need/want an automated (re-)start and a program working without user intervention use a script, an EA or an indicator.

 
Alain Verleyen #:

You can check for that conditions and warn the user to fix it. The service can still work until it's fixed. No need to remove it.

You would need to convince me it's needed, then I could ask.

EDIT: Just to be clear I don't have any power, just good reputation and relations so I can ask when I am convinced something is a good idea.

That's fine, I know how to do it, it's some extra code, nothing to worry about. Sometimes I am stuck on one thought and don't see another approach, and maybe there were another way, but...

No problem, thank you anyways.



 
Carl Schreiber #:

Well this (autostart on start of the terminal) what a service makes a service and it was offered exactly for this. Is you don't need/want an automated (re-)start and a program working without user intervention use a script, an EA or an indicator.

The point here is, if the service is malconfigured, or missing some external dependency, or some other external issue, what's the point in keeping the service up and loaded, if user interaction is required anyways.

And if everything is fine, it should continue to run,  and should be loaded on startup.

But since I cannot remove the service, I will use a GTvar and use it to my advantage, because now I will just pop up an alert, notifying the user of the issue, every time the service gets loaded.

It's an appreciative solution, I am fine with that.

But I like when things are stringent in their usage...



 
Dominik Christian Egert #:
The point here is, if the service is malconfigured, or missing some external dependency, or some other external issue, what's the point in keeping the service up and loaded, if user interaction is required anyways.

And if everything is fine, it should continue to run,  and should be loaded on startup.

But since I cannot remove the service, I will use a GTvar and use it to my advantage, because now I will just pop up an alert, notifying the user of the issue, every time the service gets loaded.

It's an appreciative solution, I am fine with that.

But I like when things are stringent in their usage...



Hmm, have you tried to jump to the end of the service program e.g. with a break within the endless loop in case of an error? Or a simple return might do it as well just like in a script?

Finally you con remove the service using the Windows tools to manage automated services - I guess.

 
Carl Schreiber #:

Hmm, have you tried to jump to the end of the service program e.g. with a break within the endless loop in case of an error? Or a simple return might do it as well just like in a script?

Finally you con remove the service using the Windows tools to manage automated services - I guess.

In fact, returning from OnStart() does the job perfectly. It will make the service go to "stopped" state.

Windows services command won't do it, it's MQ Terminal based, inside the MT Process space, governed by their scheduler/task manager...

There is no entry in Windows Services. Anyways, it would not be pure MQL5 then, and incompatible with market (and code base??)





 

Actually I would find a lot more useful to be able to start/stop or check if a service is running (easily) by code then adding/removing it which is the user responsibility (as everything similar on MT5 environment). Of course if we had also the possibility to add/remove a service by code, that would be good, but that's not really something critical at all.

The most important in my opinion would be to have some API to interact with services, from EA, indicators, or other services.

 
Alain Verleyen #:

The most important in my opinion would be to have some API to interact with services, from EA, indicators, or other services.

  • Sockets?
  • Named pipes?
  • Files on a RAM-Disk and Windows functions?
  • Memory mapped files?
Reason: