Forum Discussion

leandropoblet's avatar
leandropoblet
Frequent Contributor
11 years ago
Solved

Waiting for process to terminate fails

I'm trying to make this function work, but unfortunately I couldn't so far:




while (stillRunning)
{
if (timeOut > maxTime)
{
Log["Error"]("The App failed to shutdown correctly.");
break;
}
else
{
if (Aliases[process]["Exists"])
{
timeOut+=1000;

if ((timeOut % 1000) == 0)
{
Log["Message"]("The Application process is still running. " + (timeOut / 1000) + " seconds and waiting");
}
}
else
{
stillRunning = false;
}
}
}

Log["Message"]("The Application process has been shutdown correctly.");

}

It seems pretty obvious what it does. It would wait a maxTime checking if the application (WPF) is closed.

Now, the thing is TestComplete 9 won't recognize when the application's been closed. I mean... I can clearly see how the process is not there anymore in Task Manager whereas TC keeps counting until it reaches the limit time (more than enough in this case).

Any clues? Cheers

  • Hi Leandro,



    The code you provided executes immediately with no waiting at all.



    Try something like this:



    function TestNotepadCloseWithAlias()

    {

      var maxDelay = 10000;

      var interval = 2000;

      var stopWatch = HISUtils.StopWatch;



      stopWatch.Start();



      while(Aliases.notepad.Exists){

        Delay(interval);

        if(stopWatch.Split() > maxDelay){

          break;

        } else {

          Log.Message("Waiting for " + stopWatch.ToString() + " Will wait for application to close");

        }

      }



      stopWatch.Stop();



      if(Aliases.notepad.Exists)

      {

        Log.Error("Application did not close in time " + maxDelay + " milliseconds");

      } else {

        Log.Message("Application closed in " + stopWatch.ToString());

      }

    }



    Sincerely




4 Replies

  • simon_glet's avatar
    simon_glet
    Regular Contributor
    Hi Leandro,



    There is something in your code that I don't get: how do you mesure time ? Because when I ran it with a maxTime at 10000 it took 0,01 to execute.



    Basically if you have an application that takes 2 seconds to shutdown, the code you provided will never be running long enough to detect it.



    Sincerely
  • simon_glet's avatar
    simon_glet
    Regular Contributor
    Hi Leandro,



    The code you provided executes immediately with no waiting at all.



    Try something like this:



    function TestNotepadCloseWithAlias()

    {

      var maxDelay = 10000;

      var interval = 2000;

      var stopWatch = HISUtils.StopWatch;



      stopWatch.Start();



      while(Aliases.notepad.Exists){

        Delay(interval);

        if(stopWatch.Split() > maxDelay){

          break;

        } else {

          Log.Message("Waiting for " + stopWatch.ToString() + " Will wait for application to close");

        }

      }



      stopWatch.Stop();



      if(Aliases.notepad.Exists)

      {

        Log.Error("Application did not close in time " + maxDelay + " milliseconds");

      } else {

        Log.Message("Application closed in " + stopWatch.ToString());

      }

    }



    Sincerely




  • leandropoblet's avatar
    leandropoblet
    Frequent Contributor
    Hi Simon,



    That's exactly what I need, the code to be executed with no delays and repeatedly so it can stop as soon as the aplication stopped. It'd give me an idea of how long it took to close in every workstation without fixed delays.



    Does it make sense?



    Thanks anyway for your code. I'll try it today anyway

  • leandropoblet's avatar
    leandropoblet
    Frequent Contributor
    Hi Simon,



    At that moment in time I wasn't measuring time (actually I stripped it out just for the post).  I'm measuring it with a stopwatch , without difference in the results.



    The opposite actually happens. It takes too long to identify the process is not running.



    Here is the full code, but it doesn't have any difference in the results:



    function CheckClientIsShutdown()

    {

      var maxTime = 30000;

      var timeOut = HISUtils["StopWatch"];

      var stillRunning = true;

     

      timeOut["Start"]();

     

      while (stillRunning)

      {

        if (timeOut["Split"]() > maxTime)

        {

          Log["Error"]("The app failed to shutdown correctly.");

          timeOut["Stop"]();

          timeOut["Reset"]();

          break;

        }

        else

        {

          if (Aliases[processName]["Exists"])

          {

            Log["Message"]("The app process is still running. " + Math.round(timeOut["Split"]()) + " seconds and waiting");

          }

          else

          {

            stillRunning = false;

          }

        }

      }

      Log["Message"]("The app process has been shutdown correctly in " + Math.round(timeOut["Split"]()) + " seconds");

      timeOut["Stop"]();

    }



    So it will always stop at 30000 milisecs. Even when the app's been closed for a while.



    Sincerely