Search code examples
processautomated-teststerminatetestcomplete

Waiting for process to terminate fails


I'm working on this function to wait for a WPF app to finish.

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."); 

}

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?


Solution

  • Use this code:

    function test()
    {
      var timeout = 5000;
    
      var startTime = GetTickCount();
      var pClosed = waitProcessClose("notepad", timeout);
      var endTime = GetTickCount();
    
      var closeTimeS = Math.round((endTime - startTime) / 100) / 10;
    
      if (pClosed) {
        Log.Message("The process was closed in " + closeTimeS + " seconds.");
      }
      else {
        Log.Warning("The process was not closed in " + (timeout / 1000) + " seconds.");
      }
    }    
    
    function waitProcessClose(processName, timeout)
    {
      var endTime = GetTickCount() + timeout;
      var proc = Sys.WaitProcess(processName);
    
      while (proc.Exists) {
        var secondsLeft = Math.floor((endTime - GetTickCount()) / 1000);
        Delay(200, "Waiting for the '" + processName + "' process to be closed: " + secondsLeft);
        if (GetTickCount() > endTime) {
          Log.Warning("The process is not closed within '" + timeout + "' ms.");
          return false;
        }
      }
    
      return true;
    }