Results 1 to 4 of 4

Thread: Checking progress efficiently | C++

  1. #1
    Join Date
    Jul 2007
    Posts
    23
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Windows

    Question Checking progress efficiently | C++

    Hello everyone,

    I'm working with a function that, for instance, does some task it it's own thread and takes the address of an int for progress. Like so:

    void doSometing(int* progress)

    So, While the function is off running, I need an efficient way to check the progress for completion so that I may process the next item. I was doing something like this:

    Qt Code:
    1. while(progress < 100)
    2. {
    3. progressBar->value = progress
    4. }
    To copy to clipboard, switch view to plain text mode 

    That just seems like a bit much. But if I only check it every, say, 10 seconds, I could conceivably be wasting 9 seconds for every item. So, is there an accepted way to do something like this?

    Thanks for your time,
    Backslash
    Last edited by jpn; 21st February 2008 at 19:18. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Checking progress efficiently | C++

    You can use QTimer to do something every 10 seconds, but don't expect that you will be able to read that variable's value reliably, if you won't use some kind of synchronization.

  3. The following user says thank you to jacek for this useful post:

    Backslash (23rd February 2008)

  4. #3
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Checking progress efficiently | C++

    Why don't you use the observer pattern (or signals and slots, which already work well with threads) to automatically notify the progress bar when there's a change?
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  5. The following user says thank you to Michiel for this useful post:

    Backslash (23rd February 2008)

  6. #4
    Join Date
    Jul 2007
    Posts
    23
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: Checking progress efficiently | C++

    Thanks for the help. Also, sorry about forgetting code tags.

    I'm taking a look at the observer pattern, thanks for the tip.

    -Backslash

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.