In an IStrategy interface, there are defined "callbacks", such as onTick, onBar, etc which are executed within a special thread context, so that Order operations are valid. This guarantees serial execution of the operations, and the integrity of the API.
For Swing, there is a call to identify whether code is on the event dispatch thread.
SwingUtilities.isEventDispatchThread(), identifies whether the current thread is the Swing Event Dispatch Thread. This allows code to determine whether it can perform Swing operations, or whether it must use SwingUtilities.invokeLater(<runnable>) to perform Swing operations.
JForex API :
In a similar fashion, when code is executing on an IStrategy callback, such as void onTick(...) then it is OK to perform Order Entry API operations.
But, if code is NOT executing within an IStrategy callback, then it MUST use IContext.executeTask(<callableTask>) mechanism in order to perform the operation, on the proper thread, with the correct synchronization.
Could you please add a call to determine whether the current thread is an IStrategy callback event thread, or not?
As a suggestion, it could be in the IContext interface.
boolean IContext.isDispatchThread() // the naming isn't important
So, if an IEngine operation is being attempted, code could check like this:
IContext context = <valid context>;
// checking for thread context
if (context.isDispatchThread()) { // execute IEngine operations directly
// ok to perform order operations
}
else { // we are not in the callback context
// must use IContext.executeTask(<callable>) to perform order operations
}
Thanks very much for considering this enhancement.
HyperScalper