0

This is my main function:

var configKey = Task.Factory.StartNew(acceptConfig);
var devKey = Task.Factory.StartNew(acceptDevMode);

while (true)
{
     doSomething();

     if (configKey.IsCompleted)
     {
         getFirstChoice();
         getSecondChoice();
         getThirdChoice();

         configKey = Task.Factory.StartNew(acceptConfig);
     }

     if (devKey.IsCompleted)
     {
         setDevPassword();

         getFirstChoice();
         getSecondChoice();
         getThirdChoice();

     devKey = Task.Factory.StartNew(acceptDevMode);
     }
}

And these are my functions:

    private static void acceptConfig()
    {
        var key = Console.ReadKey(true);

        while (key.Key != ConsoleKey.C)
        {
            key = Console.ReadKey(true);
        }
    }


    private static void acceptDevMode()
    {
        var key = Console.ReadKey(true);

        while (key.Key != ConsoleKey.D)
        {
            key = Console.ReadKey(true);
        }
    }

These are basically my getChoice functions:

     var key = Console.ReadKey(true);

     switch (key.Key)
     {
       case ConsoleKey.D1
       ...

And this is my setDevPassword() function:

    private static void setDevPassword()
    {
        password = Convert.ToInt32(Console.ReadLine()); 
    }

My problem is that except for the first time either C or D is pressed, every other time the user has to press the key twice for the program to respond. In every getChoice function, the user needs to press the digit twice, and when coming back to the main while loop again, clicking C or D won't do anything on the first press - only on the second one.

Same goes for the setDevPassword() function, except there the user has to press the keys twice before the programs responses for the next key press. That means it's one more key press (that doesn't do anything) than in the other input functions.

I'm not sure if those Tasks and the IsCompleted checks are really good practice in my case, but are they the reason the user input is so "laggy"? Why is this happening?

amiregelz
  • 1,833
  • 7
  • 25
  • 46
  • In `acceptConfig()` and `acceptDevMode()` you're calling `ReadKey` twice. That's why two key presses are needed from the user. Aside from that, ReadKey is a blocking method, so why use Tasks? – 1.618 Oct 20 '13 at 03:38
  • @1.618 But why at the first press it's okay, and only afterwards 2 presses are needed? What's the problem with the functions? They're calling `ReadKey` more than one time only if it's *not* the right key press, which is fine. – amiregelz Oct 20 '13 at 08:58
  • @1.618 It also takes 2 presses in the `getChoice` functions, and I don't see any problem there as well. – amiregelz Oct 20 '13 at 14:11
  • It calls ReadKey once in the accept function, and then again in the getChoice function. It's still 2 calls even though they're not in the same function. I suggest you set breakpoints to see what lines it's on when it's waiting for a key press. Like I said before, the use of Tasks here seems messy and unnecessary. – 1.618 Oct 20 '13 at 15:48
  • @1.618 I don't see the problem...maybe it wasn't clear? I want to type `C` and then `1`, `2`, `1` for the 3 `getChoice` functions. It's okay that I will need to press `C` in order to get to the second `ReadKey` in `getFirstChoice()`, but I still in every `getChoice` function the first key press doesn't work. Also, what would you replace the Tasks with? I'm not sure what to do...Thank you – amiregelz Oct 20 '13 at 17:56

0 Answers0