The normal thing to do when you need a thread to sleep until some condition is true is to use a condition variable. The thread locks a mutex and then passes it to a condition variable and blocks (and the lock on the mutex is then temporarily released). Then, when the condition that it's waiting for is true, the thread that made that condition true signals the condition variable, and that thread wakes up, reacquiring the lock on the mutex, and continues on its merry way. The thing to watch out for though is that condition variables can have spurious wake-ups (and sometimes multiple threads can be waiting on the same condition), so there needs to be a flag of some sort to indicate when the condition is true, and when the thread wakes up from the condition variable, it checks that flag and blocks on the condition variable again if the flag isn't true. The mutex that is being used with the condition variable is also used to protect that flag.
The D runtime has a condition variable in core.sync.condition
.
There are also other good questions relating to condition variables on stack overflow such as this one: condition variable
Now, as for your particular use case, I have to concur with the comment by Idan Arye that it seems a bit off that you would be trying to do this with painting with a windowing toolkit. Normally, the way that that works is that you override the OnPaint
function (or whatever that particular toolkit calls it) and then the windowing toolkit calls the function for you. You don't tell it to paint yourself or have to worry about waiting for a condition or anything like that. So, it sounds like you may be misunderstanding how to use the windowing toolkit that you're using.