Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFuture: failed vs cancelled
Forum Updated to NodeBB v4.3 + New Features

QFuture: failed vs cancelled

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Nikita Kobzev
    wrote on last edited by
    #1

    Imagine the code working with QFuture:

    class Example : public QObject {
    public:
        Example(): QObject() {
            connect(&watcher_, &QFutureWatcher<int>::finished, this, &Example::jobFinished);
        }
    
        QFuture<int> runJobInAnotherThread() {
            QFuture<int> future = QtConcurrent::run([](QPromise<int>& promise) {
                while (!promise.isCanceled() /*&& job is not done*/) {
                    //do job
                    //!!!!MIGHT THROW QException HERE!!!!
                }
                promise.addResult(123);
            });
    
            watcher_.setFuture(future);
            return future;
        }
    
    private:
        QFutureWatcher<int> watcher_;
    
        void jobFinished() {
    
            //"normal" and "exception" flow are processed here
            //but how to process the "cancel" flow? watcher_.isCanceled() can't be
            //used, as "exception" flow also makes isCanceled() return "true"
    
            try {
                int result = watcher_.result();
                //process the result
            } catch (QException& ex) {
                //process the exception
            }
        }
    };
    
    auto example = new Example;
    QFuture<int> future = example.runJobInAnotherThread();
    //future might be cancelled by the user here
    //future.cancel()
    

    Here we have a QtConcurrent::run and a QFutureWatcher, waiting for the result on its finished signal.

    Inside the finished handler, I need to check for three different cases:

    • normal
    • background thread exception
    • future canceled by the user

    I can not find a way to check "exception" vs "cancellation", as they both do mark the future as isCanceled()=true, and they both fail to retrieve any results from the future.

    Can anyone suggest a way?

    1 Reply Last reply
    0
    • N Offline
      N Offline
      Nikita Kobzev
      wrote on last edited by Nikita Kobzev
      #6

      Seems I found a way. QFuture::isValid() can be used for what I need.

      void jobFinished() {
              if (!watcher_.future().isValid()) {
                 //future was cancelled
              }
      
              try {
                  int result = watcher_.result();
                  //process the result
              } catch (QException& ex) {
                  //process the exception
              }
          }
      
      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Hi,

        Based on the setException doc, you should check the result. It shall contain the exception.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • N Offline
          N Offline
          Nikita Kobzev
          wrote on last edited by
          #3

          The problem is not to catch an exception. The problem is to distinguish exception from cancellation.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Shouldn't you handle that using the QFuture::onFailed and QFuture::onCanceled methods ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • N Offline
              N Offline
              Nikita Kobzev
              wrote on last edited by
              #5

              Well.. Lots of code already written via QFutureWatcher. So I was expecting to achieve the goal without rewriting much.

              1 Reply Last reply
              0
              • N Offline
                N Offline
                Nikita Kobzev
                wrote on last edited by Nikita Kobzev
                #6

                Seems I found a way. QFuture::isValid() can be used for what I need.

                void jobFinished() {
                        if (!watcher_.future().isValid()) {
                           //future was cancelled
                        }
                
                        try {
                            int result = watcher_.result();
                            //process the result
                        } catch (QException& ex) {
                            //process the exception
                        }
                    }
                
                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved