Results 1 to 3 of 3

Thread: Clean shutdown console application

  1. #1
    Join Date
    Sep 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Clean shutdown console application

    Using QT 4.7 on Windows\Linux platform.

    Used QProcess to create child process in primary application and this works. The child process is a console application that looks like this;

    Qt Code:
    1. int main(int argc, char* args[])
    2. {
    3. QCoreApplication app(argc, args);
    4.  
    5. // create application objects
    6.  
    7. return app.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 

    What is the recommended approach to terminate the child application from the primary process that allows for clean-up being performed on Windows and Linux?

    Ideally I would like an approach that is event driven in the child process, as opposed to polling a status at intervals or a blocking call.

    The QProcess::terminate documentation states: "Console applications on Windows that do not run an event loop, or whose event loop does not handle WM_CLOSE message, can only be terminated by calling Kill."

    How do we know if an event loop handles WM_CLOSE message? The QCoreApplication docs does not reference specific Windows messages like WM_CLOSE.

    The QProcess::kill uses TerminateProcess (Windows) and SIGKILL signal (Linux) which wont allow application clean-up which is important in this case.

    The SetConsoleCtrlHandler method only works on Windows which is not ideal. The method is triggered by Windows CTRL_LOGOFF_EVENT and CTRL_SHUTDOWN_EVENT events who are controlled by Windows OS. This does not cover the scenario that when the parent process is closed it should close the child process.

    Appreciate any help.

  2. #2
    Join Date
    Sep 2011
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Clean shutdown console application

    Just an update.

    Invoking QProcess::terminate() in the parent process does trigger an action in QEventDispatcherWin32:: processEvents with a message containing WM_CLOSE identifer. However the message is not added to the input queue for further processing. The below if statement is false (flags is equal 36 and QEventLoop::ExcludeUserInputEvents is equal 1).

    Do not see any easy way to specify that the eventloop should handle WM_CLOSE. It is strange that the eventloop does not process this message.

    Qt Code:
    1. if (haveMessage &&
    2. (flags & QEventLoop::ExcludeUserInputEvents) ) &&
    3. ((msg.message >= WM_KEYFIRST && msg.message <= WM_KEYLAST) ||
    4. (msg.message >= WM_MOUSEFIRST && msg.message <= WM_MOUSELAST) ||
    5. msg.message == WM_MOUSEWHEEL ||
    6. msg.message == WM_MOUSEHWHEEL ||
    7. msg.message == WM_TOUCH)
    8. msg.message == WM_CLOSE))
    9. {
    10. haveMessage= false;
    11. d->queueUserInputEvents.append(msg);
    12. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by redlars; 11th September 2013 at 11:24.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Clean shutdown console application

    You parent process could create a sort of indicator file before launching the child.
    The child then uses QFileSystemWatcher to be notified about its removal and then exit when that happens.

    Another option is to use QLocalServer/QLocalSocket as an event driven communication channel between parent and child.

    Cheers,
    _

Similar Threads

  1. A problem about "undefined reference to `Clean::Clean(QObject*)'
    By GeorgeDao123 in forum Qt for Embedded and Mobile
    Replies: 4
    Last Post: 10th February 2013, 09:36
  2. Replies: 8
    Last Post: 23rd August 2012, 19:28
  3. Windows Shutdown Notification of Console Application
    By qtYoda in forum Qt Programming
    Replies: 5
    Last Post: 21st February 2012, 17:50
  4. Replies: 0
    Last Post: 14th December 2010, 00:39
  5. Replies: 2
    Last Post: 21st November 2010, 18:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.