Calling methods asynchronously

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.

Getting ready

Launch the Calculator Plus (C:Program FilesMicrosoft Calculator PlusCalcPlus.exe) and Notepad (C:Windows otepad.exe) applications.

How to do it...

We need to perform the following steps:

  1. The following function launches the calculator for factorial calculation of a hefty number. This takes up a long time, during which the calculator is unavailable for the user. Let's suppose a user has to fulfill certain operations in the meantime, and that in the Notepad.
    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");
    }
  2. After calling the 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.
  3. Now, let's replace the following string:
    button.Click();

    With the next one:

    Runner.CallObjectMethodAsync(button, "Click");

    And evoke the function again.

  4. At this time, manipulations with the Notepad would occur immediately after clicking on the button for factorial calculations in the calculator.

How it works...

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.

There's more...

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.186.167