Search code examples
cfunctionblock

How do i stop the blocking?


Hey all I've asked this question a few times in the past few days but I just don't get it...I basically want to have the while loop for the Beep command executed in the background while the user can interact with the available case statements (only one shown..there are others)....i keep getting blocked and everytime i want the beep to make a sound constantly i block the rest of my program...I have to use Beep so please don't suggest any other functionality..

here's a sample code...

while( keypress != 'q' || keypress != 'Q')
{   
    x = Beep(x);
    while (x == 1)
       Beep(350,300);

    alarm_t current;
    keypress = _getch();

    switch(keypress){

        case 'h':
            sprintf_s(current.message,"high alarm");
            current.timeOfEvent = time(NULL);
            recordEvent(current);
            break;

Now...my issue is with the while loop and the Beep command....here is what i call to Beep(x)

int Beep(int y)
{ 
    return y;
}

So basically i am trying to call a function outside of my current cpp file to just compare x and y, and return y as being equivalent to x...i thought this might avoid blocking but it doesn't...


Solution

  • Your while loop around beep just won't work and _getch is blocking. So it will just block until there's a character.

    Depending what platform you are on, you need something like kbhit (and if you google that you will find alternatives for other platforms). ie it's not standard C functionality and platform specific.

    kbhit will return true or false depending if there is a character or not.

    So you can do:

    while(!key_is_quit(ch))
    {
      Beep();
      if(kbhit())
      {
        ch = getch();
      // switch....
      }
    }