Need help understanding asynchronous function call...

 

Hi everyone, I'm trying to better understand how an EA handles asynchronous vs synchronous function calls. I edited this post to simplify my question since I didn't get any replies.

Assume I have these lines of code in an EA:

void OnInit() {
        ObjectCreate(0,obj_name,obj_type,0,0,first_price);
}

void asynchronous_price_change() {
        ObjectMove(0,obj_name,0,0,second_price);
}

double synchronous_obj_query() {
        return ObjectGetDouble(0,obj_name,OBJPROP_PRICE);
}

void test_function() {
        asynchronous_price_change();
        Print(synchronous_obj_query());
}

...the test_function returns the second price, but shouldn't it return the first price instead since the asynchronous call is supposed to wait for all other commands to be processed before executing? That's what I understand from the documentation but in practice it never does this, can someone help me understand please?

 
Jeepack:

Hi everyone, I'm trying to better understand how an EA handles asynchronous vs synchronous function calls. I edited this post to simplify my question since I didn't get any replies.

Assume I have these lines of code in an EA:

...the test_function returns the second price, but shouldn't it return the first price instead since the asynchronous call is supposed to wait for all other commands to be processed before executing? That's what I understand from the documentation but in practice it never does this, can someone help me understand please?

Wrong, the synchronous call will wait...by definition an asynchronous function doesn't wait.

The documentation state :

An asynchronous call is always used for ObjectMove(), that is why the function only returns the results of adding the command to a chart queue. In this case, true only means that the command has been successfully enqueued, but the result of its execution is unknown.

To check the command execution result, you can use a function that requests object properties, such as ObjectGetXXX. However, you should keep in mind that such functions are added to the end of the queue of that chart, and they wait for the execution result (due to the synchronous call), and can therefore be time consuming. This feature should be taken into account when working with a large number of objects on a chart.

So calling the ObjectGetDouble() is a synchronous call which force the queue to be executed, so ObjectMove() is executed firstly and ObjectGetDouble() then returns second_price.

 
Alain Verleyen #:

Wrong, the synchronous call will wait...by definition an asynchronous function doesn't wait.

The documentation state :

So calling the ObjectGetDouble() is a synchronous call which force the queue to be executed, so ObjectMove() is executed firstly and ObjectGetDouble() then returns second_price.

Ohhhh I see what you mean, I was really confused there and understanding this completely the wrong way. Thanks for the help Alain!

Reason: