Thread: Problem with while and if-else loop, not responding ?

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    14

    Problem with while and if-else loop, not responding ?

    Following my previous question How to execute Time Loop in C without clock() and timer ?
    I have a while loop, which calculates elapsed time and time when I hit key to display different output.

    Code:
    void drawImage(int now, int last_event, int delay){
    
    
        arUtilTimer();
        now = arUtilTimer();
        
        while(1)
        {
            
            if ((now - last_event) >= delay)
            {
                argDrawImage(image1);
                last_event = now;
            }
        else
            {
                argDrawImage(image2);
                now = arUtilTimer();
            }
        }
    
    
    }
    Which I'm going to call later in my main loop. But I keep get not responding result. The code working fine and the calculation also working fine if I don't call my while loop. Is there any mistake in my while loop ?
    Last edited by pixie_laluna; 10-14-2018 at 09:18 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,317
    Well the first problem is that once the code enters your while(1) loop, that's the end of the show as far as the rest of your code is concerned.

    The other problem I suppose is that your arUtilTimer doesn't offer much benefit over using the regular time functions. At the very least I would have expected to see some kind of callback interface where you could arrange for a function to be called at an interval.

    In essence, you can only have one while(1) loop at the top level of your code, and everything within it needs to be non-blocking.
    Code:
    while ( 1 ) {
      // non blocking key detection
      if ( keyPressed() ) {
        // read key
        // do something
      }
      if ( timeExpired() ) {
        // do draw code
      }
      // short sleep, approximately matched to your video rate
      // 16ms is ~60Hz
      // 20ms is 50Hz
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    14
    Hi, thanks ! I'm aware that while(1) loop here in an endless loop. But that's my purpose, this loop will be executed all the time when the program is running. And I just need different condition to push different output. About the arUtilTimer, I use the arUtilTimer for another purpose as well in this program, and I would rather keep this than adding new library, so I dont have too many libraries running. I don't really understand about non-blocking key. And I already have my keyEvent defined in other function like this :
    (I cannot put code on reply, so I put picture) .

    Problem with while and if-else loop, not responding ?-screen-shot-2018-10-15-8-19-57-png

    Should I put my keyEvent inside the loop ?
    Last edited by pixie_laluna; 10-14-2018 at 05:36 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Once you enter that while(1) loop, there's no getting out of it, so any code anywhere else is not going to do anyone any good unless you call it inside that while loop. I wouldn't necessarily expect you to put that code inside your while loop, but your infinite loop needs to at least see whether input has come in and fob it off on the key-handler.

    Because you also have some time-sensitive code, you can't just sit around and wait until somebody presses a key; that means that whatever function you use for that purpose can't "block" the rest of the loop from happening. (To use standard C as an example, scanf() is a blocking input mechanism, since once you hit that line of code you're going to keep sitting there until the user hits enter and the OS gives your program the input; you'll need to use some other kind of input that will just immediately return with no input if there's no input ready.)

  5. #5
    Registered User
    Join Date
    Oct 2018
    Posts
    14
    it's been a few hours and unfortunately I still don't understand this blocking / non-blocking.
    I did try to put short sleep function as Salem suggested. But still confused about this blocking.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,317
    How is keyEvent called at the moment?

    A non-blocking call is one which simply returns after some short amount of time, having done what it was supposed to do, or discovered that there is nothing it can do at the moment.

    So your draw image should be perhaps
    Code:
    void drawImage(int delay){
      now = arUtilTimer();
      if ((now - last_event) >= delay) {
        if ( ? ) {
          argDrawImage(image1);
        } else {
          argDrawImage(image2);
        }
        last_event = now;
      }
    }
    Your higher level function can keep calling this because it no longer blocks, and can carry on responding to key events like it does at the moment.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Stops Responding.
    By KittenAqua in forum C++ Programming
    Replies: 5
    Last Post: 12-16-2011, 05:26 PM
  2. Not Responding
    By osal in forum Windows Programming
    Replies: 0
    Last Post: 07-15-2004, 11:43 AM
  3. (Not Responding)
    By Mithoric in forum Windows Programming
    Replies: 1
    Last Post: 06-26-2004, 09:12 AM
  4. responding to system prompt?
    By daddio in forum Windows Programming
    Replies: 10
    Last Post: 09-24-2001, 01:54 PM
  5. responding to system prompt
    By daddio in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 09-18-2001, 11:55 PM

Tags for this Thread