Forum Discussion

ash10's avatar
ash10
Occasional Contributor
10 years ago

How to check if application is closed

I want to check if exe application is closed before opening another instance of application.

Right now I am using script like.

 

WMI["ComputerName"] = ".";
WMI["WaitForProcessExit"]("MyApplication.exe", 30000)

 

MyApp= TestedApps["MyApplication"]
MyApp["Launch"] = true
MyApp["Run"](1,true,-1);

 

After I click on close applicaton I call this script and then open another instance of application.

 

But some times it take more time  to close the

existing  application and so the above script does not work.

 

6 Replies

    • ash10's avatar
      ash10
      Occasional Contributor

      I do not want to wiat based on time as time might vary based on PC

      • Manfred_F's avatar
        Manfred_F
        Regular Contributor

        Of course it's safer, quicker and much more elegant to wait for status changes instead of waiting for "magic" time values.

        But You can't avoid completely using time thresholds: what happens, if your application hangs up during shutdown? How will You detect it?

        It won't give You a status "hung up". So, You choose a maximum time for closing, double it, and wait. If in the meantime, the application is gone, You exit successful. If the waiting time elapses, You exit without success.

         

        It is always the same with asynchronous communication: You never know, whether the partner didn't respond or whether the reply was lost. Your only chance is to wait and then react.

  • m_essaid's avatar
    m_essaid
    Valued Contributor

    hi,

     

    here is the procedure I use to kill all instances of a program.

    it's up to you to modify the "If not closedthen" line...

     

    procedure KillProcessus(NomProcessus: string);
    var
      p, IsClosed;
    begin
      p := Sys.FindChild('ProcessName', NomProcessus);
      while p.Exists do
      begin
        p.Close;
        IsClosed:= p.WaitProperty('Exists', False);
        if not IsClosed then
          p.Terminate;
        p:= Sys.FindChild('ProcessName', NomProcessus);      
      end;  
    end;

  • Hello ash10,

     

     

    That's a problem which has bugged me for quite some time. Here is what I did:

     

    1 - I close the application and open it again every time I start a Test item, like this:

     

    /*Terminates the application, for suuuuureee :) */

    Sys.OleObject("WScript.Shell").Run("TASKKILL /F /IM myApplication.exe");

    /*Wait 3 seconds, just for preventing the application from crashing, because you just killed it ;)*/

    aqUtils.Delay(3000);

    /*Launch the application*/

    Sys.OleObject("WScript.Shell").Run("myApplication");

     

    2 - I terminate the application every time a Test Item is finished.

    /*Terminates the application, for suuuuureee :) */

    Sys.OleObject("WScript.Shell").Run("TASKKILL /F /IM myApplication.exe");

     

    Some docs that inspired me: http://support.smartbear.com/viewarticle/56565/

    kill, kill, kill

     

    That works perfectly fine to me. Let me know if that works for you too.

     

    Regards,

     

    Leandro de Araújo Souza

  • joe_2's avatar
    joe_2
    Contributor

    VBScript snippet:

     

    <command to close the app here>

    Do While Sys.WaitProcess("ApplicationName", 1000).Exists = True

        Delay(1000,"Waiting for the application to close")

    Loop

    <commands for whatever comes next here>

     

    When it finishes that loop, the application is closed.

    If you feel the need, you can add a counter to the loop, and declare an error if it gets too big, to detect a hung app at shutdown.

    As-is, though, this will just sit there looping until it doesn't detect the application, then it will exit the loop.

    Each time through the loop lasts a second, approximately.