Timer and Threads

Apr 22, 2008 at 4:01pm
Hi everyone,

I need some help. I am creating a tool where the user has 10 seconds to do something before a message pops up.

I am using the timer from a previous post in this forum: http://www.cplusplus.com/forum/beginner/317/

The problem is when i start the timer (e.g. for 10 seconds) the program 'freezes' and no interaction can happen within the program until the 10 seconds is up and the timer is stopped.

Does this mean i have to implement the timer within a thread or is it to do with the rest of my code.

Many Thanks
Nick

Apr 22, 2008 at 4:11pm
After further reviewing i beleive the problem is that i am using a while loop and this is why the program 'freezes' while the loop is active.

The code i am using is:
1
2
3
4
5
6
7
timer t;
t.start();
while (t.isRunning()) {		
	if (!t.isOver(10)) {
	              t.stop();
	}
}

I have it set so that the timer runs for 10 secs then stops. The while loop is used to check that the timer is running.

Is there another way without using a while loop.

Nick

Apr 22, 2008 at 5:08pm
try starting the timer before the loop.
Apr 22, 2008 at 7:09pm
Yes, you would probably have to use a thread for that.
Apr 22, 2008 at 10:46pm
I have already created my program and do not want to edit it too much.

Is there a quick and easy way of implementing a new thread. All i need to be able to do is have a countdown of 10 seconds while still alowing the user to use my program. At the end of the countdown a msg box will pop up.

Thanks,
Nick
Apr 22, 2008 at 11:31pm
Yes, just make a new thread like this:

 
HANDLE thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) InsertYourTimerFunctionHere, NULL, 0, NULL); //<-- do as it says... 


I believe you need windows.h to do that. Also, define your function like this:

1
2
3
4
int WINAPI YourFunction() {
     //timer stuff etc
     return 0;
}
Topic archived. No new replies allowed.