asked on
#include <conio.h>
#include <time.h>
#include <string>
#include <iostream>
std::string timout_input(unsigned int secs)
{
std::string s;
time_t t1 = time(NULL) + secs;
while(t1 > time(NULL))
{
if(_kbhit())
{
char c = static_cast<char>(_getch());
if('\r' == c || '\n' == c) {
std::cout << std::endl;
break;
}
std::cout << c;
s += c;
}
}
return s;
}
int main( void )
{
std::string const & s = timout_input(5);
std::cout
<< "You entered: "
<< s
<< std::endl;
}
ASKER
ASKER
#include <windows.h>
#ibclude <stdio.h>
DWORD WINAPI My30SecWaitThread ( LPVOID) {
// timeout after 30secs
Sleep ( 30 * 1000);
printf("Time Out");
ExitProcess(0);
return 0;
}
void main () {
DWORD dwTID;
CreateThread(NULL,0,My30SecWaitThread, 0, 0, &dwTID);
getchar();
}
ASKER
#include <windows.h>
#ibclude <stdio.h>
DWORD WINAPI My30SecWaitThread ( LPVOID) {
// timeout after 30secs
Sleep ( 30 * 1000);
printf("Time Out");
ExitProcess(0);
return 0;
}
void main () {
DWORD dwTID;
HANDLE hThread = CreateThread(NULL,0,My30SecWaitThread, 0, 0, &dwTID);
getchar();
TerminateThread(hThread,0); // cancel thread
}
ASKER
#include <conio.h>
#include <time.h>
#include <string>
#include <iostream>
#include <windows.h>
std::string timout_input(unsigned int secs)
{
std::string s;
time_t t1 = time(NULL) + secs;
while(t1 > time(NULL))
{
if(_kbhit())
{
char c = static_cast<char>(_getch());
if('\r' == c || '\n' == c) {
std::cout << std::endl;
break;
}
std::cout << c;
s += c;
t1 = time(NULL) + secs; // Reset timeout
}
Sleep(0); // Yield (give other threads/processes a chance)
}
return s;
}
int main( void )
{
std::string const & s = timout_input(5);
std::cout
<< "You entered: "
<< s
<< std::endl;
}
ASKER
#include <windows.h>
#include <stddef.h>
#include <stdlib.h>
#include <conio.h>
#include <process.h>
#include "stdafx.h"
#include <afx.h>
#include <string.h>
#include <stdio.h>
#include <afxdb.h>
DWORD WINAPI WaitThread ( LPVOID ) {
// do something after 30mins
//Sleep ( 30 * 60 * 1000);
Sleep (100);
printf("Time Out");
fflush(stdout);
ExitProcess(0);
return 0;
}
int main(int argc, char* argv[], char* env[])
{
HANDLE hThread;
DWORD dwTID;
int i;
while (true) {
i=getchar();
hThread = CreateThread( NULL,0, WaitThread, 0, 0, &dwTID);// cancel thread
printf("T: %x \n",hThread);
fflush(stdout);
putchar(i);
TerminateThread(hThread,0); // cancel thread
}
return 0;
}
ASKER
while (true) {
hThread = CreateThread( NULL,0, WaitThread, 0, 0, &dwTID);// cancel thread
Sleep(0);
i=getchar();
printf("T: %x \n",hThread);
fflush(stdout);
putchar(i);
TerminateThread(hThread,0); // cancel thread
}
#include <windows.h>
#include <stddef.h>
#include <stdlib.h>
#include <conio.h>
#include <process.h>
#include "stdafx.h"
//#include <afx.h>
#include <string.h>
#include <stdio.h>
//#include <afxdb.h>
DWORD WINAPI WaitThread ( LPVOID ) {
// do something after 30mins
//Sleep ( 30 * 60 * 1000);
Sleep (5sd000);
printf("Time Out");
fflush(stdout);
ExitProcess(0);
return 0;
}
int main(int argc, char* argv[], char* env[])
{
HANDLE hThread;
DWORD dwTID;
int i;
while (true) {
hThread = CreateThread( NULL,0, WaitThread, 0, 0, &dwTID);// cancel thread
Sleep(0);
i=getchar();
printf("T: %x \n",hThread);
fflush(stdout);
putchar(i);
TerminateThread(hThread,0); // cancel thread
}
return 0;
}
ASKER
#include <windows.h>
#include <stddef.h>
#include <stdlib.h>
#include <conio.h>
#include <process.h>
#include "stdafx.h"
#include <afx.h>
#include <string.h>
#include <stdio.h>
#include <afxdb.h>
long lTimeout;
DWORD WINAPI WaitThread ( LPVOID ) {
// do something after 30mins
//Sleep ( 30 * 60 * 1000);
Sleep (lTimeout);
printf("Time Out"); fflush(stdout);
ExitProcess(0);
return 0;
}
int main(int argc, char* argv[], char* env[])
{
HANDLE hThread;
DWORD dwTID;
int i;
lTimeout=1000;
if (argc >1) lTimeout = atol(argv[1]);
printf("Timeout %d\n",lTimeout);fflush(stdout);
while (true) {
hThread = CreateThread( NULL,0, WaitThread, 0, 0, &dwTID);// cancel thread
Sleep(10);
i=(getchar)() ;
putchar(i);
if (i== 0x3) return(0);
fflush(stdout);
TerminateThread(hThread,0); // cancel thread
Sleep(10);
}
return 0;
}
#include <windows.h>
#include <stddef.h>
#include <stdlib.h>
#include <conio.h>
#include <process.h>
#include "stdafx.h"
//#include <afx.h>
#include <string.h>
#include <stdio.h>
//#include <afxdb.h>
long lTimeout = 1000;
HANDLE hEvent = CreateEvent(NULL, true, false, NULL);
DWORD WINAPI WaitThread ( LPVOID )
{
SetEvent(hEvent);
Sleep(lTimeout);
printf("Time Out"); fflush(stdout);
ExitProcess(0);
return 0;
}
int main(int argc, char* argv[], char* env[])
{
if(argc >1) { lTimeout = atol(argv[1]); }
printf("Timeout %d\n",lTimeout);fflush(stdout);
for(;;)
{
HANDLE hThread = CreateThread( NULL,0, WaitThread, 0, 0, 0);
WaitForSingleObject(hEvent, INFINITE);
int i = getchar();
TerminateThread(hThread,0);
putchar(i);
if (i== 0x3) return(0);
fflush(stdout);
}
return 0;
}
ASKER
C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.
TRUSTED BY
Theoretically, that should work. The catch is that even if you specify a 'TimerProc()', your app will still need a message loop in order for 'SetTimer()' to work (there used to be a MSDN article that stated "SetTimer() was not designed to be used with a console application because it requires a message loop to dispatch the timer signal to the timer procedure. In a console application, this behavior can be easily emulated with a thread that is set to wait on an event.". The options now are to either create a thread that periodically triggers the code that you want to be executed periodically or use 'CreateWaitableTimer()' instead. For the latter, see http://support.microsoft.c
Open in new window