Loading...
 

Waiting for Results

 
Most of the operations that a module offers are coming in two different flavours: One where the actual result of the operation is awaited and one where you only receive a "ticket" while the operation is finishing in the background. With the ticket you're able to check the operations result at a later time. In most of the cases you will only need the first flavour. But let's see how these flavours look like and what is the intention to have them.

Awaiting a Result

If you can wait for the call to a modules operation to complete you would write code like this:

string response = SomeFakeMethod(...); // Lasts as long as the operation needs to complete, might take seconds.
jsonLib.Deserialize(response);
isSuccess = jsonLib["isSuccess"].ToBool();
// Go ahead in MQL code

Thats pretty fine if you are able to wait for the call to finish.

Checking the Result at a later Time

Let's imagine you built an Expert Advisor that calculates very important stuff for your business. This Expert Advisor might reach a point, where you would like to share some results via mail or Telegram. BUT: Because the Expert Advisor needs to immideatly go on with further calculations you might not wait for the call to a modules operation to finish (because sending a mail might take the one or other second, depending on network problems, server capacity, ...). Therefore most of the operations have a equivalent starting with a "Start" prefix:

// Might take the one or other second to return because the full operation is awaited to finish
string resultOfSendText = SendText("-1001167825793", "Some text");
// vs. Only the successful start of a operation is awaited.
string resultOfSendText = StartSendText("-1001167825793", "Some text");

When using a "Start" mothod a ticket (called the correlation key) will be generated and reported as a result to the caller so that you can go on in your MQL code. In the background a thread will do the rest of the work and save the result internally for you. You can ask periodically for the result with the help of the ticket number you earned. This way the very important stuff that your Expert Advisor does can be calculated and sometime later you can check whether sending the mail was successful and if you need to resend it again.

string response = StartSomeFakeMethod(...);
jsonLib.Deserialize(response);
bool correlationKey = jsonLib["correlationKey"].ToStr();
// In the meanwhile: Do all the important stuff and then:
string realResult = GetMessageByCorrelationId(correlationKey);  // might be done periodically if the result is not computed yet
jsonLib.Clear();
isSerializable = jsonLib.Deserialize(realResponse);
isSuccess = jsonLib["isSuccess"].ToBool();
// ...