Problem with setting timeout in C++
C / C++ / MFC
4
Posts
3
Posters
0
Views
1
Watching
-
What do you mean with timeout ? What kinod of application : Win32, MFC, or simple C++ project with standard libs ? ~RaGE();
-
You want something like this?
#include <iostream>
#include <conio.h>
#include <ctime>
using namespace std;time_t g_Time = 0;
unsigned int g_uInterval;void SetTimeout(unsigned int uX)
{
g_uInterval = uX;
}void Begin()
{
g_Time = time(0);
}bool IsTimeout()
{
if( g_uInterval < difftime(time(0), g_Time) )
return true;
return false;
}void main()
{
unsigned int uX = 20;
cout << "Press any key in " << uX << " sec.\n";
SetTimeout(20);
Begin();
while(1)
{
if(_kbhit()) {
cout << "Key pressed.\n";
break;
}
if(IsTimeout()) {
cout << "Time out.\n";
return;
}
}
getch();
}Maxwell Chen