Killing several instances of a tested application

Sometimes, there arises a necessity to terminate several processes under one and the same name, while the number of open processes may vary; hence, the exact number of the processes to terminate remains unknown.

Getting ready

Add the Calculator Plus application to TestedApps and launch its copies (three or more).

How to do it...

In order to terminate several instances of the tested application we need to perform the following steps:

  1. Launch the following function:
    function testTerminateProcesses()
    {
      var procName = "CalcPlus";
      while(Sys.WaitProcess(procName, 10).Exists)
      {
        Sys.Process(procName).Terminate();
        aqUtils.Delay(500);
      }
    }
  2. In the result of the function call, all the instances of Calculator Plus application will be terminated.

How it works...

Since we do not know the exact number of launched processes, we resort to the loop, in which, with the help of the WaitProcess method, we check against the availability of any of the sought processes. If such a process is located, we then terminate it with the Terminate method and wait the half a second (500 milliseconds) that might be necessary for the termination to complete. Then, we check again for any outstanding processes running under the same name.

Exiting the loop takes place when there is no process under the given name.

There's more...

We can make this function more universal by declaring procName as a parameter (and removing declaration of the variable in the very beginning of the function):

function terminateProcesses(procName)

Now we can call this function for termination of any of the processes, not just of the Calculator Plus termination, for example:

terminateProcesses("notepad");

If you need to close only one process, provided you know its index, it is possible to do it the following way:

Sys.Process("CalcPlus", 2).Terminate();

Here, 2 stands for the index of the process.

See also

  • Here we used the WaitProcess method for verifying the process' existence. The Wait methods are explained in details in the Waiting for an object to appear recipe in Chapter 5, Accessing Windows, Controls, and Properties.
..................Content has been hidden....................

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