Sometimes, there arises a necessity to launch some by-process in the tested application (for example, some long-drawn mathematical calculation), and to simultaneously carry out other actions, unhindered. In this recipe we will consider how this can be achieved.
Launch the Calculator Plus (C:Program FilesMicrosoft Calculator PlusCalcPlus.exe
) and Notepad (C:Windows
otepad.exe
) applications.
We need to perform the following steps:
function testAsynch() { var wCalc = Sys.Process("CalcPlus").Window("SciCalc", "Calculator Plus"); wCalc.Keys("123321123321"); var button = wCalc.Window("Button", "n!"); button.Click(); var wNotepad = Sys.Process("notepad").Window("Notepad"); wNotepad.Activate(); wNotepad.Keys("some text"); }
Click
method for the button of the factorial calculation, our function will keep waiting until the calculator is made available again (about 20 seconds, after which a window shows up with a warning). Only afterwards would the Notepad window be enabled with some text inputted into it.button.Click();
With the next one:
Runner.CallObjectMethodAsync(button, "Click");
And evoke the function again.
The Runner.CallObjectMethodAsync
method accepts two parameters: the object
and the callee
method. If the method takes these parameters, they should be passed on to the CallObjectMethodAsync
method immediately after the name of the evoked method.
In the result, the method of the object will be called asynchronously, and the thread of execution will be relegated to the line of the script that follows.
The CallObjectMethodAsync
method does not create a new thread of execution, which in turn implies we cannot, as an example, launch the method aqFile.Copy
asynchronously in order to copy a large file. Use of this method is recommended only for calling private methods of the tested application.
If, upon execution of several actions, we still need to wait for the operation to be completed, having been launched asynchronously, we could resort to the value that is to be returned by the CallObjectMethodAsync
method:
var res = Runner.CallObjectMethodAsync(button, "Click"); // some actions res.WaitForCompletion(20000);
The WaitForCompletion
method has one mandatory parameter: maximal waiting time.
18.189.186.167