Results 1 to 6 of 6

Thread: Windows Shutdown Notification of Console Application

  1. #1
    Join Date
    Aug 2010
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Windows Shutdown Notification of Console Application

    I'm running a QCoreApplication as a TCP/IP server. I've implemented a signal handler to get SIGINT and SIGTERM but I'm not getting notification when a user restarts or shuts down Windows (XP).

    It works fine when the application receives a shutdown command from a TPC/IP client.

    But I need to save files when the user shuts down Windows. How can I do this?

    I have implemented the aboutToQuit() signal, but this doesn't seem to get called either.

    Thanks

  2. #2
    Join Date
    Aug 2010
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Windows Shutdown Notification of Console Application

    Is this a Qt problem on Windows that is known to have no solution?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Windows Shutdown Notification of Console Application

    No, that is not a Qt problem on Windows. What does Qt have to do with your system shutting down? Does Windows even send SIGINT or SIGTERM to processes when closing the system or did you just assume that if Unix does this, so does Windows?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Dec 2010
    Location
    Russia
    Posts
    83
    Thanks
    1
    Thanked 12 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Windows Shutdown Notification of Console Application

    Hm , actually aboutToQuit() is being emitted just fine...unless you terminate an application.So make sure that you use either
    void QCoreApplication::exit ( int returnCode = 0 ) [static] or void QCoreApplication::quit () [static slot]. I think i used to have alike problems with implementation of pretty much the same feature once , but in my case the thing was that i could not handle close event right , while an application was in tray or smth. like that...But again , it was 100 % due to my ignorance rather then QT's dysfunctionality.

  5. #5
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Windows Shutdown Notification of Console Application

    @qtYoda

    There is a way to be notified when the user closes the console or hits Ctrl+C. I have tried it and it works; you might want to try it to see if it also works when the user shuts down Windows without closing the console first (it should according to MSDN). I did the following:
    1. upon initializing the application, call SetConsoleCtrlHandler() to register a handler that will get called when the console is closed or Ctrl+C is hit;
    2. in the handler, forward the notification to Qt's event loop by using e.g. QCoreApplication::postEvent() with a dedicated custom event, then return TRUE;
    3. do the heavy work upon receiving the custom event, then exit using e.g. QCoreApplication::quit().
    Note that it is simpler than the equivalent in Unix, where you cannot do much in a signal handler. There are a number of solutions to that issue, notably using a pipe, or running sigwait() in a dedicated thread, or using the Linux-specific signalfd() system call.

  6. #6
    Join Date
    Aug 2010
    Posts
    28
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Windows Shutdown Notification of Console Application

    Thanks everyone. Here is what I've tried.

    When I posted I had already tried 1) connecting via "aboutToQuit" signal, and 2) the signalhandler method to get the users Ctrl-C method. Neither seemed to be getting informed when the user just shutdown or restarted via the standard "Start/Turn Off Computer" method.

    So I started thinking that, since the program has no window (and no console), Windows would not inform anything. I found some online documentation that Microsoft provides a method to create a hidden "window" that is there just for the purpose of getting windows messages. So I used RegisterClassEx() and CreateWindowEx() to create the non-window "window" and hooked into the message loop with that "window". Inside the message loop I write a simple message to to file so I can see when the function is called. I know it's being called in normal situations when Windows is telling the window to shutdown via WM_QUIT/VM_CLOSE/VM_DESTROY. But its NOT calling the message hook when the user chooses to shutdown/restart. Argghhhh!

    I'm begining to think that, since what I'm really doing is what is normally thought of as a "Windows Service", that I need to just implement this as a Windows Service and hook into the management paradigm for services. Any other thoughts before I start down that road would be appreciated if they might shed light on getting Windows to simply let me know when it intends to kill my program.

Similar Threads

  1. Replies: 4
    Last Post: 25th April 2011, 12:43
  2. Replies: 0
    Last Post: 14th December 2010, 00:39
  3. Replies: 2
    Last Post: 21st November 2010, 18:03
  4. Reboot/Shutdown Windows with startDetached
    By NoRulez in forum Qt Programming
    Replies: 0
    Last Post: 11th March 2009, 12:40
  5. Windows focus / Windows Shutdown Problems
    By December in forum Qt Programming
    Replies: 6
    Last Post: 22nd October 2007, 14:10

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.