Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem with setting timeout in C++

Problem with setting timeout in C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++helptutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ArFuk
    wrote on last edited by
    #1

    Can anyone pls tell me how to implement a timeout function in C++?? thx very much

    R M 2 Replies Last reply
    0
    • A ArFuk

      Can anyone pls tell me how to implement a timeout function in C++?? thx very much

      R Offline
      R Offline
      Rage
      wrote on last edited by
      #2

      What do you mean with timeout ? What kinod of application : Win32, MFC, or simple C++ project with standard libs ? ~RaGE();

      A 1 Reply Last reply
      0
      • R Rage

        What do you mean with timeout ? What kinod of application : Win32, MFC, or simple C++ project with standard libs ? ~RaGE();

        A Offline
        A Offline
        ArFuk
        wrote on last edited by
        #3

        maybe a simple C++ project~ can you pls help?? thx very much

        1 Reply Last reply
        0
        • A ArFuk

          Can anyone pls tell me how to implement a timeout function in C++?? thx very much

          M Offline
          M Offline
          Maxwell Chen
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups