Okay, then in that case you can start a timer, set with the duration of the period you'd like the program to run for. When you get the WM_TIMER message, you know this period has elapsed and it's time to exit.
You can use the following console-mode program to achieve this. On my system, it consistently runs for between 5.02 and 5.07 seconds.
The way I've written it, code in the "do stuff here" section won't be interrupted when the time elapses, you must continually perform small parts of the task to be completed in order to check to see if a WM_TIMER message has come in yet. I.e you might calculate the colour of a single pixel of an image, or a 1/10 of a second of audio or whatever.
Without knowing what you're trying to actually do for N number of seconds, I can't know if it's worth spinning up another thread to actually do stuff while the main thread does nothing other than waiting for timer messages.
#include <cstdio>
#include <windows.h>
int main()
{
MSG msg;
int shouldContinue = true;
bool msgFound = false;
DWORD curThreadId = GetCurrentThreadId();
while (PostThreadMessage(curThreadId, WM_USER, 0, 0) == 0)
{
Sleep(10);
}
int nMilliSeconds = 5 * 1000;
SetTimer(NULL, 0, nMilliSeconds, NULL);
while (shouldContinue == true)
{
msgFound = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
if (msgFound == true)
{
if (msg.message == WM_TIMER)
shouldContinue = false;
}
else
{
printf(".");
}
}
return 0;
}