The page where you read about threads is here:
Each script and EA runs its own thread. Only indicators share a common thread with the MT4 GUI. I'm well aware of this, which I why I avoid using while loops in all my indicators, so that the indicator doesn't go into an infinite loop and freeze MT4's GUI.
I use several WinAPI functions in my scripts and indicators. Only PostMessageW has caused a problem so far.
....sigh....I wonder what the problem is with PostMessageW in a script. I haven't tested it in an EA. Does it work without crashing the terminal?I
As I said before: "Consider using your code in an EA and this way the PostMessageW command will not crash your MT4."
My situation is this:
I would like to place an indicator on the chart just as I'm about to place a trade. I don't want the indicator running constantly. I would like to press a hotkey and then have the indicator be instantly placed on the chart without it showing the config dialog box. I would like the script to be linked to the hotkey, to run just once and perform just this very task - placing the indicator on the chart and getting rid of the dialog box without any manual input from me. The reason I go through the trouble of trying to get a script to do all this is to get rid of the config dialog box. I've got multiple charts open and I don't want to be clicking OK to get rid of the dialog box all the time. One hotkey press and the indicator should be up and running. That's what I want.
So you're saying that PostMessageW only crashes the terminal when it's called from a script? Why does it work in an EA and not in a script? What is the deeper programmatic reason for this?
Probably you can prepare a template with this indicator (and required parameters) and then just switch to/from this template when needed. Hence you don't need any scripts, Windows messaging, or other automation.
FYI, it seems that checks have been added into MT4 (build 1170, possibly earlier) to see if MQL_TRADE_ALLOWED is false (e.g. for indicators) and if so calls to ChartOpen/ChartApplyTemplate that apply an EA will have the EA's Common parameter variables changed such that trading is not allowed without manually adjusting those parameters.
I found that using Windows messaging (PostMessageA/MT4_WMCMD_LOAD_TEMPLATE from indicator) was easiest way to resolve my specific code but an MQL4 only solution could be done as well if the calling code were all moved to an EA (that has MQL_TRADE_ALLOWED).
See https://www.mql5.com/en/forum/191697#comment_10577960 for more
discussion and solutions to handle this specific change in
Just open the EA on any chart and save it as a template.
Then for future use, when you want to reopen the EA, then open any chart with ChartOpen or ChartSetSymbolPeriod and apply the template
The EA will be loaded as it is defined as a part of the template.