Results 1 to 5 of 5

Thread: Threads (follow-up to "Screen refresh during heavy computation")

  1. #1
    Join Date
    Aug 2008
    Location
    Cherry Hill, NJ USA
    Posts
    61
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Threads (follow-up to "Screen refresh during heavy computation")

    My initial problem was to allow the main window to refresh during heavy computation that couldn't be broken into smaller chunks. Based on information from wysota, that item is resolved - the computation is in its own thread, and the main thread continues to process events.

    The problem now is that, if the main thread is killed, I haven't been able to cleanly kill the computation thread. I've tried numerous combinations of catching the QCloseEvent and calling terminate() on the thread, QApplication::closeAllWindows(), ::exit(), etc.

    I'm using a Win32 console window during development (via AllocConsole()) and one thing I noticed is that the console window's "x" stops all the threads. Is there a straightforward platform independent way to obtain that effect in Qt?

    Thanks in advance!
    Martin

  2. #2
    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: Threads (follow-up to "Screen refresh during heavy computation")

    Make your computation thread check some global variable from time to time and if it is true, return from run(). Then you can raise that flag at any time from your main thread.

    If injecting code into the thread is not an option for you (for instance when the thread simply executes one long function that you can't modify), your only option is to wait for it to finish or kill the process (as you do by closing the console window).

  3. #3
    Join Date
    Aug 2008
    Location
    Cherry Hill, NJ USA
    Posts
    61
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Threads (follow-up to "Screen refresh during heavy computation")

    Thanks for the reply.

    Killing the process would be preferable to waiting for the computation to end. (It is very tricky stuff, in Fortran, that I'd rather not disturb.)

    Is there a platform-independent way to kill the process using Qt calls, or do I need to use native O/S calls? I'm supporting both Windows and Linux...

    Martin

  4. #4
    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: Threads (follow-up to "Screen refresh during heavy computation")

    Quote Originally Posted by martinb0820 View Post
    Is there a platform-independent way to kill the process using Qt calls, or do I need to use native O/S calls? I'm supporting both Windows and Linux.
    Divide something by 0

    For posix systems you can use kill passing it your own pid and SIGKILL as the signal. For non-posix systems you have to find other means.

  5. #5
    Join Date
    Aug 2008
    Location
    Cherry Hill, NJ USA
    Posts
    61
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Lightbulb Re: Threads (follow-up to "Screen refresh during heavy computation")

    The following is effective in Windows. I just didn't want to have to resort to an #ifdef!

    void VisGui::closeEvent(QCloseEvent *event)
    {
    cout << "Received closeEvent" << endl;

    #ifdef WIN32
    // http://support.microsoft.com/default...;en-us;q178893
    TerminateProcess(
    OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE, _getpid()),
    0);
    #endif

    ///\todo Equivalent for Linux
    }

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.