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.
Add the Calculator Plus application to TestedApps and launch its copies (three or more).
In order to terminate several instances of the tested application we need to perform the following steps:
function testTerminateProcesses() { var procName = "CalcPlus"; while(Sys.WaitProcess(procName, 10).Exists) { Sys.Process(procName).Terminate(); aqUtils.Delay(500); } }
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.
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.
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.18.219.81.43