Skip to content
This repository has been archived by the owner on Dec 12, 2023. It is now read-only.

Canceled future will still call finished() even through they're canceled #13

Closed
vpicaver opened this issue Aug 5, 2019 · 0 comments
Closed

Comments

@vpicaver
Copy link
Contributor

vpicaver commented Aug 5, 2019

In my use case, a class maybe called to do work multiple time. It's generally useful to start an processing the data, but if new data is submitted, the class restarts the processing and cancels previous runs. For example:

class TestClass {
    public:
        void doWork() {

            if(m_doWorkFuture.isRunning() || m_doWorkFuture.isStarted()) {
                m_doWorkFuture.cancel();
            }

            int currentCount = m_count;
            auto runFuture = QtConcurrent::run([currentCount](){
                return currentCount + 1;
            });

            m_doWorkFuture = runFuture;

            m_subscribeFuture = observe(runFuture).subscribe([this](){
                m_finishCount++;
            },
            [this](){
                m_cancelCount++;
            }).future();

        }

        void waitToFinish() {
            await(m_subscribeFuture);
        }

        QFuture<void> m_subscribeFuture;
        QFuture<int> m_doWorkFuture;
        int m_count = 0;
        int m_finishCount = 0;
        int m_cancelCount = 0;
    };

    TestClass myTest;
    myTest.doWork();
    myTest.doWork();
    myTest.doWork();
    myTest.waitToFinish();

    QCOMPARE(myTest.m_finishCount, 1);
    QCOMPARE(myTest.m_cancelCount, 2);`

This test currently fails on the master on windows.

vpicaver added a commit to vpicaver/asyncfuture that referenced this issue Aug 5, 2019
vpicaver added a commit to vpicaver/asyncfuture that referenced this issue Nov 4, 2019
@vpicaver vpicaver closed this as completed Mar 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant