Robot stopping - multi threading issues ??

Created at 16 May 2014, 14:44
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!
IA

ianj

Joined 14.05.2013

Robot stopping - multi threading issues ??
16 May 2014, 14:44


I have a Robot that maintains a link to an external process over a network socket. This network socket is opened, read and written from a ThreadPool.QueueUserWorkItem derived thread (initiated from OnStart) . This seems to work fine but when it tries to retrieve a symbol it hangs a short while later. The code loops in the thread on a blocking network read - its doesn't return until the Robot has been stopped

e.g.  Symbol sym = MarketData.GetSymbol("XAUUSD")

outputs 16/05/2014 11:26:12.014 | Symbol 'XAUUSD' not found.

A standalone Robot seems to manage this ok - Is it possible that the environment has been de-stabilised by the additional use of threads.

This does not cause problems when the operation is attempted in a standalone simple Robot and simply avoiding XAUUSD does not provide a solution - another pair will likely trip it up a few pairs later. Its interesting that it successfully manages to retrieve the symbols for several pairs before it fails

I have wrapped all code in try catch Exception wrappers - and exception is not thrown - the thread just stops

 


@ianj
Replies

ianj
16 May 2014, 14:51

I added a few C# synchronisation "lock" calls to ensure that the 2 pool spawned threads (one reads the socket for external command, the second scans and reports the status of positions) cannot interfere with each other and/or On* event callbacks and it seems to have stabilised - at least for now. The threads are not locking and the only Symbol not found errors now come from unsupported pairs - as expected.

This would suggest that the Spotware C# API calls are not threadsafe

Please confirm if you think his is likely the case and i will continue to protect from threaded execution explicitly in my Robot


@ianj

Spotware
16 May 2014, 15:19

cAlgo.API isn't thread safe. You have to avoid working with API objects and methods in different threads.


@Spotware