No-locking wait function?


#1

Hi!
I want to show an image for a few miliseconds with a wait function without let the rest of
the code wait. Is there any way to do that? Ich think a thread would be oversized for this…


#2

Since C++11 you could do this:

    #include <future>
    ....
    auto handle = std::async(std::launch::async, [&]()
    {
        {
            const MessageManagerLock mmLock(Thread::getCurrentThread());
            if (!mmLock.lockWasGained())
                return;

            // Show image
        }

        std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Sleep for 100ms

        {
            const MessageManagerLock mmLock(Thread::getCurrentThread());
            if (!mmLock.lockWasGained())
                return;

            // Hide image
        }
    });

    // Code that should not be blocked

    handle.wait(); // Wait for image to be hidden again.

#3

Actually, what you want is a different thread that will display your image, wait for 100ms and then hide it again. There are many ways to do that. Juce itself got a thread class from which you can inherit and C++11 got some fancy new toys (http://en.cppreference.com/w/cpp/thread).


#4

Please don’t follow the above advice!

In a case as trivial as this, two things NOT to do are:

  1. Use threads. When doing pure GUI work, threads are generally best avoided, and totally unnecessary in a case like this

  2. Block the message thread and “wait”. You mustn’t think about this in terms of “waiting”. What you actually want to happen is to create some GUI that displays your image, and to start a timer which will remove/delete the GUI after a given time has passed. Nothing in GUI code should ever spin or wait, or block.

The only class you need to do this is Timer. Or maybe the SplashScreen class will already do what you need?


#5

Thanks very much!
The Timer was what I searched for!