0

我想在应用程序运行时停止卸载它。我想通过视觉工作室设置和部署来做到这一点。我在安装程序类中添加了以下代码,然后在操作列表中调用,但它不起作用。

 public static bool ApplicationIsBusy()
    {
        Process application = null;
        foreach (var process in Process.GetProcesses())
        {
            if (!process.ProcessName.Contains("applicationname")) continue;
            application = process;
            break;
        }

        if (application != null && application.Responding)
        {
            return true;
        }

        else
        {
            return false;
        }

    }
    public override void Uninstall(IDictionary savedState)
    {

      base. Uninstall(savedState);
        if (ApplicationIsBusy()==true)
           throw new ApplicationException("Prevent uninstall while application busy.");
    }

  protected override void OnBeforeUninstall(IDictionary savedState)
    {
        base.OnBeforeUninstall(savedState);

        if (ApplicationIsBusy()==true)
            throw new ApplicationException("Prevent uninstall while application busy.");
    }
4

1 回答 1

1

尝试这个,

Method to check if application is running

private bool ApplicationIsBusy()
{
   bool isApplicationRunning = false;

   Process[] applicationProcess = Process.GetProcessesByName("applicationname");
   if (null != applicationProcess && applicationProcess.Length > 0)
   {
       isApplicationRunning = true;
   }

   return isApplicationRunning;
}

Uninstall method implementation

public override void Uninstall(IDictionary savedState)
{
  if (ApplicationIsBusy())
      throw new Exception("Prevent uninstall while application busy.");

  base. Uninstall(savedState);
}
于 2013-10-19T12:02:17.877 回答