"MetaTrader has stopped working...". Have you encountered this problem? Thank you for entering my topic.

 

The MQL program (script or Expert Advisor) calls the Visual C++ DLL for network communication, the DLL interface function creates a thread to wait for and receive data, when the socket has data arrival, the thread receives the data and then inform the script or Expert Advisor to display it in the chart. When the chart is closed, if the sender continues to send data, the problem occurs in the terminal as follows:

 

 

 

When the chart is closed, the MQL program (script or Expert Advisor) end, but the thread that create by DLL interface function does not exit, in this case, if the sender continue to communicat with the thread, the problem above occurs; if the thread exit before the closing of the chart, that problem does not occurs. How can you exit the thread that create by the DLL interface function before the chart is closed? 

 
You must have a function to kill the thread in the DLL and have the EA call it.
 
WHRoeder:
You must have a function to kill the thread in the DLL and have the EA call it.
When and where I call the function to kill the thread in the DLL? As we know, the OnDeinit function will be called when the chart is being closed, so there two ways as follows:

1. The best way to terminate a thread is make it's thread function return, In the OnDeinit function, I call a DLL interface function to change the loop condition of the thread, so that the thread will exit itself.

2.  In the OnDeinit function, I call a DLL interface function which call the Windows API TerminateThread function to terminate the thread.

The two ways above can not solve that problem, and the Sleep function can not work in the OnDeinit function. 
Reason: