Discussion Locked

setting timer option in C or C++

0 helpful
11 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
Is it possible to write a program in C or C++ which will run only for specified amount of time. (for example while conducting an online exam the Question paper should appear only for specified amount of time from starting time.) Please tell me thecode for this.

11 replies

by //.ichael g

What platform?
Win32 has some SetTimer functions that can either do a callback or send a WM_TIMER message to the client.

by cedric king

The simplest I guess would be to perform a Do While Loop while waiting for the user input. The condition would be either the user enters something which breaks from the Loop or when the time is up. For the time portion, just have a variable prior entering the Loop storing THAT time then in the Loop get the current time. If Current Time greater than the Started Time by the specified amount of time, then exit Loop too.
Hope you understand what I meant. It's basically a Loop function where two conditions are to be met for it to continue.

by lastorck

i think so (Win32):

CreateProcess(...,&procInfo);
WaitForSingleObject(procInfo.hProcess, timeout);
TerminateProcess(procInfo.hProcess, 0);

Regards

by lastorck

maybe so (inside process):

//...
HANDLE p = GetCurrentProcess();
GetProcessTimes(p, ...);
//...
tm = localtime(&t);
//....
if(...)
ExitProcess(0);

Regards

This discussion is closed to new replies.