Have you pressed a key yet?



  • while (true)
    {
        if (Console.KeyAvailable)
        {
              key = Console.ReadKey()
              // do logic here with the new key here
         }
         Thread.Sleep (100);
    }
    

    In case you aren't familiar, KeyAvailable is a bool, which basicly tells you if a call to ReadKey is going to block (until a key is available). This of course is useful if you want a non blocking loop. I think that point was lost on whoever wrote this.



  • Obviously, he should hav done it like this:

    while (true)
    {
        switch (Console.KeyAvailable)
        {
        case true:   
    
          key = Console.ReadKey()
          // do logic here with the new key here</pre><pre>    case false:</pre><pre>          Thread.Sleep (100);
    
    }
    

    }

    So it won't wait for 100ms between keypresses!



  • @geocities said:

    Obviously, he should hav done it like this:

    while (true)
    {
        switch (Console.KeyAvailable)
        {
        case true:   
    
          key = Console.ReadKey()
          // do logic here with the new key here</pre><pre>    case false:</pre><pre>          Thread.Sleep (100);
    
    }
    

    }

    So it won't wait for 100ms between keypresses!

    I liked your missing break statement there.  Of course if you fixed that small bug, teh codez would be perfect!




  • @DanceMaster said:

    This of course is useful if you want a non blocking loop. I think that point was lost on whoever wrote this.
    Someone told him that it was best practice to not call blocking methods from a UI thread. More proof that good programming cannot be taught.


Log in to reply