【问题标题】:Waiting for process to terminate fails等待进程终止失败
【发布时间】:2014-06-05 07:39:04
【问题描述】:

我正在开发此功能以等待 WPF 应用程序完成。

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

}

现在,TestComplete 9 无法识别应用程序何时关闭。我的意思是...我可以清楚地看到任务管理器中不再存在该进程,而 TC 一直计数直到达到限制时间(在这种情况下已经足够了)。

有什么线索吗?

【问题讨论】:

    标签: process automated-tests terminate testcomplete


    【解决方案1】:

    使用此代码:

    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;
    }
    

    【讨论】:

    • 嗨,Dmitry,感谢您的回复和代码。我想要做的是避免使用 WaitProcess 方法,因为我不想要任何“固定”时间。这会让我知道每个工作站需要多长时间才能关闭。
    • WaitProcess 方法默认立即生效,不等待。如果寻找的进程不存在,则该方法立即返回。
    • 我明白,但目标是拥有一个能够在不同上下文中使用它的功能。该方法可以用于某些用途,我的功能旨在做更多的事情,例如测量打开关闭应用程序需要多长时间并在中间发布警告/消息。这是该功能的剥离版本,没有所有这些功能,但即使它以完全相同的方式工作。
    • 我已经更新了答案。现在,样本计算进程关闭所花费的时间。这是你需要的吗?
    • 就是这样!我今天会试一试,但它看起来很有希望!再次感谢
    猜你喜欢
    • 1970-01-01
    • 2020-04-21
    • 2013-12-26
    • 2020-09-09
    • 1970-01-01
    • 2021-11-07
    • 1970-01-01
    • 2013-11-08
    • 1970-01-01
    相关资源
    最近更新 更多