0

I'm trying to open a server via batch file in cmd.exe in my Qt application. Despite I'm using QProcess::startDetached() to start the command line it closes immediately after start. The server is starting, but instead of "serving" the process is killed. Here is my code:

void DICOMReceiver::startReceiver()
{
    QProcess receiver;
    boost::filesystem::path dbDir = boost::filesystem::absolute(databaseDirectory.toStdString());
    receiver.startDetached("cmd.exe", QStringList() << "/c" <<
                           "dcmrcv.bat" << "AETitle:11112" << "-dest " << dbDir.string().c_str());
    receiver.waitForStarted();
}

When I run the batch file manually in the cmd.exe it is working as desired. Does anybody have an idea how to keep the process running so that I can use the server?

El_Mewo
  • 99
  • 8

1 Answers1

1
  1. startDetached is a static function. You don't need a process instance.

  2. You should pass a working directory to startDetached. For all I know it "closes" because the batch file doesn't exist where it's looking for it.

  3. Your waitForStarted() call is a no-op since the startDetached method does not know anything about your receiver instance. You simply wrote obfuscated C++ that deceives you. There is no way to wait for a detached process to start when using Qt. A detached process is fire-and-forget.

  4. Do not use waitForXxx methods, as they block the thread they're in, and make the UI unresponsive. Use signal-slot connections and write asynchronous code instead.

So, your method should be fixed as follows:

void DICOMReceiver::startReceiver()
{
  boost::filesystem::path dbDir =
    boost::filesystem::absolute(databaseDirectory.toStdString());
  // FIXME
  const QString batchPath = QStringLiteral("/path/to/the/batch/file");
  QProcess::startDetached("cmd.exe", QStringList() << "/c"
                          << "dcmrcv.bat" << "AETitle:11112" << "-dest "
                          <<< dbDir.string().c_str(), batchPath);
}
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • Thank you for your answer, but it's still closing immediately after start. I see the output of the server rushing over the screen and then it disappears. I tried to start it without the other arguments, too, but that's also not the problem. When I start just the cmd.exe it works (even with my first version). – El_Mewo Mar 24 '14 at 20:00
  • Then the problem is not with your code, but with the batch file exiting early. Substitute a dummy batch file that simply shows its arguments and does a `pause` at the end. It will work. Then slowly start adding stuff from `dcmrcv.bat` to it. It's really debugging 101 and has nothing to do with Qt or C++ at this point. – Kuba hasn't forgotten Monica Mar 25 '14 at 16:36
  • Seems illogical to me, because the batchfile isn't closing when I call it manually in cmd.exe and it also doesn't close when I create a link to it, giving it the needed arguments, and simply run the link per doubleclick. It is just closing when using QProcess. But I'll give it a try. – El_Mewo Mar 26 '14 at 08:15
  • Thank you for your answer. One argument was -dest Directory. I treated it as two arguments. That was a mistake... – El_Mewo Mar 26 '14 at 17:14
  • @El_Mewo See, a dummy batch file would have shown you that. It's a debugging technique that comes very handy sometimes :) – Kuba hasn't forgotten Monica Mar 26 '14 at 17:33