Understanding Asynchronous and Synchronous Processes
Desktop Agent is a process that performs controlling on an application running in another process.
Most of time, you can develop projects without this knowledge, but it can be useful to understand the concept of asynchronism and multitasking implemented in Desktop Agent.
This article briefly describes the multitasking architecture of Desktop Agent. It then presents some questions submitted by developers, where the answers relate to asynchronism and multitasking.
Desktop Agent Multitasking Architecture
Desktop Agent consists of the following components:
-
A Listener thread that listens for technical events notified by Desktop Agent connectors
-
Received events are stored in an Events queue
-
A Dispatcher thread which peeks events from the Events queue and injects them into the JavaScript engine
-
A JavaScript engine which receives injected events and executes associated Event handlers
Use Cases
What Happens when Desktop Agent Starts?
When Desktop Agent starts:
-
It injects all JavaScript files of your project into the JScript engine
-
JScript engine then executes all the global code (code outside functions) written in the injected scripts
-
It triggers the GLOBAL.START event in the Desktop SDK
-
Desktop SDK then executes all of the Event handlers associated with this event, one by one
Example:
What Happens when Desktop Agent Receives a Technical Event?
When Desktop Agent receives a technical event from a Desktop Agent connector:
-
It stores the event in its internal Events queue
-
The Dispatcher thread waits until an event is available in the internal Events queue When an event is available:
-
It synchronously inject this event into the Desktop SDK running in the JScript engine
-
Desktop SDK then executes all the event handlers associated with this event, one by one
-
It waits until the Desktop SDK stops handling the event
Can I be Interrupted During Code Execution?
Is there a risk that a piece of code (that is, handling of an event) will be interrupted by the execution of another piece of code (for example, handling of another event, timeout) ?
The JavaScript engine is mono-tasked, so a sequence of code can never be interrupted.
Generally speaking, only one callback is executed at one time. You must return from a callback before another callback can be executed.
Example:
In the following example:
You will see log messages in the following order:
Are Functional Events Delivered Synchronously?
When a Functional event is notified using the notify method, is the Functional event handler executed synchronously or asynchronously?
Functional events are delivered asynchronously.
That means that the Functional event handler is not executed during execution of the notify method.
Sometimes, Controlling Fails Although the Page Exists
Occasionally, control action on MyPage.MyItem fails, stating that MyPage is unknown, while MyPage.exist() returns true.
When the Desktop Agent connector detects that a declared MyPage closes, it immediately notifies an UNLOAD event.
This event is processed by the Framework, which updates its list of living MyPage instances. A call to MyPage.exist() method then returns false.
As mentioned above, processing of the UNLOAD event is delayed until the previously received events have been processed completely. During processing, a call to MyPage.exist() method returns true, while MyPage doesn’t exist any more.
Can I Miss Events During Step Transition?
In a scenario, I open a page in one step and wait until this page opens in the next step. Can I miss the reception of the LOAD event during step transition?
The endStep method synchronously executes the next step callback.
An Event handler set in this callback will be armed before you exit the current handler. This ensures that you handle the LOAD event.
Example:
Are control actions Executed Synchronously?
When I perform a control action (get, set, and so on), is it executed synchronously? Must I wait between two control actions?
When you perform a control action:
-
Desktop Agent transmits this action to the Desktop Agent connector and waits until it returns.
-
The Desktop Agent connector receives the action, executes it, and then returns
So, generally speaking, all the control actions are executed synchronously. There is one exception with the click() action.
In some applications, clicking a button can lead to long processing times:
-
Load a large amount of data
-
Perform a long calculation process
-
And similar
To avoid Desktop Agent being locked while waiting for the end of the processing, Desktop Agent connectors execute the click() action asynchronously and return immediately.
So, after a click(), it is not recommended to execute control actions without waiting until the controlling application has finished its job.
What is the Best Method to Delay control actions?
What is the best method to wait some small time between control actions? Is it not recommended to use ctx.sleep method to wait?
Desktop SDK provides two methods for waiting for a given time:
-
The ctx.wait method waits asynchronously for a given time:
-
The ctx.sleep method waits synchronously for a given time:
The drawback of using the ctx.sleep method is that Desktop Agent is frozen during the waiting time:
-
No event can be received by Desktop Agent.
-
Any Desktop Agent connector that wants to notify a technical events is locked.
Using the ctx.wait method doesn’t freeze Desktop Agent: it is the safest and recommended way to wait for a given time.
If you want to use ctx.sleep(), make sure that:
-
You wait for a short time to let the controlled application do its job
-
No Technical event is generated during this time
Otherwise, you may encounter the well-known Server busy message.
I Sometimes Get the ‘Server Busy’ Message
I sometimes get the 'Server busy' message. Where does it come from, and why?
Communication between Desktop Agentand its connectors takes place synchronously using the COM protocol:
-
Desktop Agent sends control actions to perform to its connectors
-
The connectors send technical events to Desktop Agent
If an exchange takes too long, the COM protocol falls in timeout and the ‘Server busy’ message is displayed.
If you encounter this message, check whether the last executed control order does not take too long:
-
The ctx.sleep() call
-
Controlling an item that takes time to be recognized (see Controlling Entities Using the UI Automation Connector)
-
and so on
What is the Best Method to Implement a Waiting Loop?
I need to implement a waiting loop to wait until something happens in the controlled application. What is the best way to do that?
Because your waiting loop may take a significant amount of time, you must use an asynchronous method to implement your waiting loop.
1 – Use the ctx.polling method
The best way is to use the ctx.polling method provided by the Desktop SDK.
The ctx.polling method implements an asynchronous polling loop:
When you call the ctx.polling method:
-
Desktop SDK calls your ‘test’ callback:
-
If it returns true, it calls your ‘done’ callback
-
Else, it starts a timer with the given delay
-
-
When the timer ends, Desktop SDK calls your ‘test’ callback:
-
If it returns true, it calls your ‘done’ callback
-
Else, if the nbMax turns is raised, it calls your ‘fail’ callback
-
Else, it starts a timer with the given delay
-
During the delay between the start and the end of the timer, Desktop Agent is free to receive and manage events.
2 – Use a Functional event
If, for any reason, you cannot use the ctx.polling method, you can implement an asynchronous loop using a functional event:
This pattern was used before the ctx.polling method existed. It is still efficient and given here for your information, but using ctx.polling is easier.
3 – Don’t use ctx.sleep() method
Event if you are tempted, don’t use the ctx.sleep() method:
This pattern has several drawbacks, the most severe being that it locks Desktop Agentfor the entire duration of the loop. In any case, don’t use this pattern (otherwise there may be problems).