Download MetaTrader 5

Terminal auto restart.

To add comments, please log in or register
antony
549
antony  

Hi

I am quite new to coding and was wondering is there a function that can restart the terminal as there is a function on the live update that restarts the terminal?

Thanks

Antony

qjol
3243
qjol  

only by using WINAPI

whroeder1
15934
whroeder1  
If you had used this you would have found enough in five minutes to write:
#import "shell32.dll"
   int ShellExecuteA(int hWnd, string Verb, string File, string Parameter, string Path, int ShowCmd);
#import
bool Shell(string file, string parameters=""){
    #define DEFDIRECTORY NULL
    #define OPERATION "open"    // or print
    #define SW_SHOWNORMAL 1
    int r=ShellExecuteA(0, OPERATION, file, parameters, DEFDIRECTORY, SW_SHOWNORMAL);
    if (r > 32) return(true);
    Alert("Shell failed: ",r);
    return(false);
}
void CloseTerminal() {
    int main = GetAncestor(WindowHandle(Symbol(), Period()), 2/*GA_ROOT*/);
    PostMessageA(main, WM_CLOSE, 0, 0);
}
void RestartTerminal(){
    if ( Shell("RestartTerminal.bat") ) CloseTerminal();
}
RestartTerminal.bat:
@echo off
set term="C:\IBFX Trader 4\terminal.exe"
:loop
tasklist | findstr "terminal.exe" >nul
if ERRORLEVEL 1 start "terminal" %term% & exit
ping -n 6 localhost >nul& rem Sleep 5 seconds
goto loop
Untested

whroeder1
15934
whroeder1  

See also https://www.mql5.com/en/forum/141029

If the Script is put in the terminal directory then

Not compiled, not tested.

@echo off
rem RestartTerminal.bat
:loop
  tasklist | findstr "terminal.exe" >nul
  if ERRORLEVEL 1 start "terminal" "%~dp0terminal.exe" & exit
  ping -n 6 localhost >nul& rem Sleep 5 seconds
goto loop
void RestartTerminal(){
    string termPath    = TerminalPath(),
           termRestart = termPath + "\\RestartTerminal.bat"
    if ( Shell("cmd.exe", "/C "+termRestart) ) CloseTerminal();
}
Not compiled, not tested.
The above will not work for multiple installs. For that:
@echo off
rem RestartTerminal.bat
set /a initialCount=0
for /f "usebackq delims==" %%F in (`tasklist ^| findstr "terminal.exe"`) do (
    set/a initialCount+=1
)
:loop
    ping -n 6 localhost >nul& rem Sleep 5 seconds
    set /a newCount=0
    for /f "usebackq delims==" %%F in (`tasklist ^| findstr "terminal.exe"`) do (
        set/a newCount+=1
    )
    if %newCount% == %initialCount%     goto loop
start "terminal" "%~dp0terminal.exe"
void RestartTerminal(){
    string termPath    = TerminalPath(),
           termRestart = termPath + "\\RestartTerminal.bat"
    if ( Shell("cmd.exe", "/C "+termRestart) ){
       Sleep (10000);
       CloseTerminal();
    }
}
nooby
5
nooby  

Hi WHRoeder, Nooby here.

When i use metaeditor to create a script, how should the format be for the implementation of your code above (latest, for multiple installs)? Would greatly appreciate your help if you could show me through the newly created, empty script below.


//+------------------------------------------------------------------+

//| terminal autorestart.mq4 |
//| Copyright 2012, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"

//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
To add comments, please log in or register