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?