I’m looking for a simple cross platform way to call a function after a certain amount of time with the option of killing it early.
Similar to how javascript works with time = window.setTimeout("alert('Hello');", 500); and if(time) clearTimeout(time)
Is this possible in basic C++ without any additional (non standard) libraries or framework?
If so can you give me a pointer in the right direction?
—
The reason I am doing this is I have a DB query that takes 3/4 seconds the first few times and display a wait cursor for this, however it looks a little silly/distracting after the query is cached and the cursor blinks to wait and then blinks back to normal!
Thanks for your time!
Calling a function is a blocking operation. I can think of a couple ways to approximate the behavior you want…
Time Slicing:
Make the function execute a short segment of work which furthers it’s overall goal incrementally. You can call it repeatedly until x time has passed.
Multithreading:
Run the function in another thread. But, to synchronize the results of the function between threads may take understanding of multithreading you don’t have. If you really wanted to kill the operation after a certain duration you could forcibly shutdown the thread – but you probably don’t want to do that. You probably want to let it finish, whenever it finishes (it won’t be blocking the main thread so it shouldn’t annoy the user).
The GUI library you’re using may make the later option easy, but I don’t know what you’re using.