Thread Safety
How Does a cBot/Indicator Run¶
cTrader runs each indicator/cBot on a separate sub-process isolated from the cTrader desktop main process. This means that all new instances of indicators and cBots run on separate processes.
This approach allows cTrader to impose code access restrictions on separate processes, improving safety and stability.
Furthermore, separate indicator/cBot processes do not share their resources with the main cTrader desktop process. If something goes wrong with an indicator or a cBot, the underlying issue will not have an effect on cTrader itself.
Is the API Thread-Safe¶
In brief, no. Only a few methods in the Automate API are thread-safe, meaning you can call them from any thread. Everything else should run on the cBot/indicator main thread.
How to Run Something in the cBot/Indicator Main Thread¶
To run something on the main thread of a cBot/indicator, use the BeginInvokeOnMainThread()
method. It accepts an action delegate and you can call it from any running thread.
Using Static Properties/Fields to Share Data Between Indicator/cBot Instances¶
Yes, you can. As each indicator/cBot instance runs on a single child process, you can use static properties or fields to share data between the different instances of the same cBots/indicators.