How to wait on BeginInvokeOnMainThread.

Created at 09 May 2022, 12:53
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
DA

darcome

Joined 23.01.2019

How to wait on BeginInvokeOnMainThread.
09 May 2022, 12:53


Hello everyone, is it possible to wait on BeginInvokeOnMainThread so that I know when the code inside ends and I can read the results?

At the moment I am using ManualResetEventSlim to sync between my code and the code inside BeginInvokeOnMainThread, but I was wondering if spotware could provide a safer way to do it.

 

Thanks in advance!


@darcome
Replies

amusleh
10 May 2022, 09:37 ( Updated at: 10 May 2022, 09:42 )

Hi,

BeginInvokeOnMainThread executes your code as a promise in future without blocking, asynchronously on cBot/Indicator main thread.

It passes your action delegate to the cBot/Indicator main thread event loop.

What do you mean by waiting? do you mean blocking the calling thread until the execution of your passed code to BeginInvokeOnMainThread finish? 

There is only one way to wait, block your current thread by using a thread synchronization context like ManualResetEventSlim until cBot/Indicator thread execute your code.

You can avoid some of this issues, but the solutions are dependent on your circumstance and there is no general solution.


@amusleh

darcome
10 May 2022, 10:15

Ok, as I wrote, this is what I am already doing.

I was hoping BeginInvokeOnMainThread could become an async function, so that it could be awaited.

 

Thank you anyway for the answer!


@darcome

amusleh
10 May 2022, 10:59 ( Updated at: 10 May 2022, 11:06 )

RE:

darcome said:

Ok, as I wrote, this is what I am already doing.

I was hoping BeginInvokeOnMainThread could become an async function, so that it could be awaited.

 

Thank you anyway for the answer!

Hi,

Awaiting doesn't solve the issue, the code that calls BeginInvokeOnMainThread is executed on one thread and the action delegate you pass to BeginInvokeOnMainThread will run on cBot/Indicator main thread.

Even if you await, the cBot/Indicator thread has to either execute the continuation code by itself or dispatch it to the calling thread, during this time the calling thread will be free to do other stuff which is the benefit of asynchronous programming as you can do some other task while your passed action delegate to BeginInvokeOnMainThread is waiting to be executed.

To solve this issue you can create a pub/sub messaging agent, with a post method that will take your action delegate to BeginInvokeOnMainThread, and once the action delegate is executed by cBot/indicator main thread the agent will notify you, you can use .NET channels to avoid thread synchronization.

You can use such a wrapper over BeginInvokeOnMainThread to even get retuned values from your code that will be executed on cBot/Indicator main thread.

 


@amusleh

darcome
10 May 2022, 12:02

Ok, thank you very much for your answer


@darcome