Resurrection
Resurrection

Reputation: 4106

How to start detached process and wait for the parent to terminate?

Using QProcess to implement an updater I start a detached process from my application and immediately exit. In the spawned updater process I will overwrite the files as needed and then start the main app again. The problem is that the update of the files can sometimes fail if the main app does not close "fast enough" to release all the loaded libraries before the updater starts overwriting them. One way would be to wait for arbitrary amount of time like 1 sec and then start updating but I would rather implement something that actually checks if the parent process is not running anymore. I can pass its ID when I spawn it but that does not really cut it because there seems to be no function such as bool QProcess::isRunning(qint64 pid). I also don't think it is possible to connect signals and slots cross applications... Any ideas?

Upvotes: 3

Views: 1306

Answers (1)

Dmitry Sazonov
Dmitry Sazonov

Reputation: 8994

You can use a QSystemSemaphore class in both applications to wait.

app:

int main()
{
  QSystemSemaphore sem( "some_uuid", 1, QSystemSemaphore::Create );
  sem.acquire();
  // ...
  app.exec();

  // TODO: start updater
  // sem.release(); // not sure, that it will not be done automatically
  return 0;
}

updater:

int main()
{
  QSystemSemaphore sem( "some_uuid", 1, QSystemSemaphore::Open );
  sem.acquire(); // Will wait for application termination
  // ...
  app.exec();
  return 0;
}

You should not forget about error handling. And you should try to open and close file "yourapp.exe" from "updater.exe" with full access to be sure, that application is closed.

For 100% result it is prefferable to use platform-specific API.

Upvotes: 4

Related Questions