N
N
nadom2014-11-16 21:14:39
Programming
nadom, 2014-11-16 21:14:39

How to organize parallel processes in C++?

Essence:
the function displays information on the screen.
switching to the next screen is performed after 5 seconds or by pressing any button.
those. i need to somehow separate key = getch(); and sleep(5000);
C++, console

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Armenian Radio, 2014-11-16
@nadom

The idea is to wait a short time (0.1s), then check for the presence of a character in the keyboard buffer, if it is - stop, if not - repeat the cycle.

#include <conio.h>
#include <iostream>
#include <windows.h>

using namespace std;

bool waitKey(const size_t secondsToWait)
{
    int i=secondsToWait*10;
    for(;(i)&&(!_kbhit());i--)
    {
         Sleep(100);
         if(i%10==0)
         {
             cout<<i/10<<endl;
         }
    }
    //очистка буфера клавиатуры
    while(_kbhit())
    {
        getch();
    }
    return(i);
}

int main()
{
  cout<<"press any key..."<<endl;
  if(waitKey(5))
  {
      cout<<"Countdown was stopped by key" << endl;
  }
  else
  {
      cout<<"Countdown was not stopped"<<endl;
  }
  return 0;
}

D
DancingOnWater, 2014-11-17
@DancingOnWater

Solution for Linux:
stackoverflow.com/questions/2984307/c-key-pressed-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question