Is there a better alternative to Console.ReadKey()? - c#

Most programming books will tell you to use Console.ReadKey() to pause the console, but is there a better alternative?
Console.WriteLine("Press any key to continue.");
Console.ReadKey();

You haven't actually told us what you wish to achieve.
If you wish to stop the output until the user chooses to continue, then you're not really going to get much better than just waiting for a key to be pressed using Console.ReadKey. If you just want to pause the output for a certain amount of time, you can use the Thread.Sleep method, which doesn't require any human invtervention.

How about Console.ReadLine() :)

system("pause");
does the same thing

Related

How to get rid of the ReadKey() que if a person where to spam a key?

My console application uses ReadKeys thruout the code; if a person where to say click a key when an ReadKey is not active, it would to my speculation be added to a que of sorts, so when the code gets to the readKey it instantly goes on the previus keypress and continues as if that key was pressed.
Is there a way to fix this?
Edit: To clarify, axidental keypresses affect the ReadKeys that have not yet been run
You can check if there is any key available in the input stream and then read the next key.
A very good solution can be found here: Clear Console Buffer
Another similar solution here: https://newbedev.com/csharp-c-console-clear-input-buffer-code-example
// while there are characters in the input stream
while(Console.KeyAvailable)
{
// read them and ignore them
Console.ReadKey(false); // true = hide input
}
// Now read properly the next available character
Console.ReadKey();

Is this behavior due to possible async?

Salutations,
I am fairly new to C#, I feel comfortable with Java, and thus I feel decently comfortable with C# (massive fan of its LINQ and SQL syntax). Anywho, I am however a total novice to asynchronous calls, and I was wondering if the following behavior is related to this.
So in my code I have the following:
string sentence;
Console.WriteLine("Enter your sentence: {0}", sentence = Console.ReadLine());
Console.WriteLine("Sentence is: {0}", sentence);
Console.ReadKey();
It is very simple, I was just trying to see if I could insert the reading of the input within the same prompt that asks for it. However, what I notice is that the code runs, but it is a blank screen, I can type my input and then it would display what I typed, and wait for a key click in order to close the CMD window.
What I was wondering is the following: Is this a sort of async behavior -- if not, what is it then? And if so, is it possible to have the ReadLine() within the same Console.WriteLine() or do I have to have them as two separate lines as one would normally do?
There is nothing about asynchronous here.
The first Console.WriteLine requires its parameter(s) to get ready before its own execution. In other words, Console.ReadLine has to execute before the first Console.WriteLine. That is where the blank screen come from.
Just write as
Console.WriteLine("Enter your sentence: ");
sentence = Console.ReadLine()
Console.WriteLine("Sentence is: {0}", sentence);
or in "the same line"
Console.WriteLine("Enter your sentence: ");
Console.WriteLine("Sentence is: {0}", Console.ReadLine());
Synchronous and Asynchronous is not the concern here, just syntax error try
Console.WriteLine("Enter your sentence: ");
Console.WriteLine("Sentence is: {0}", Console.ReadLine());

C# Console Application Individual Line Loading

I'm currently working in C# in visual studio 2010. I am working on console applications and was wondering if anyone knew a way to have Console.WriteLines appear on the console individually without key strokes? E.g. text that appears line by line. I've looked around quite a bit and can't seem to find a way to do it without using standard command lines rather than C#.
Thanks in advance.
For each string you want on its own line, write that string to the console.
string a = "hello";
string b = "world";
Console.WriteLine(a);
Console.WriteLine(b);
Try to put
Console.ReadKey()
between your line outputs. This will wait for any key to be pressed after every line.
Example:
Console.Write("Press any key to continue ...");
Console.ReadKey(true);
Console.WriteLine("DONE!")
This will print "Press any key to continue ..." then wait for any keystroke and add "DONE!" to the end of the line.
Thanks to everyone who replied, sorry it look my a long time to get back to this, I've been extremely busy. Sorry that my question was poorly phrased. What I was attempting to do was create an image with console.WriteLine();
I found that the way to do this was with system threading to create a delay between lines.

I'm trying to to use ReadLine(), or Read() or ReadKey to read in integers using C#

I'm new to C# and I'm having some trouble reading some ints out of a Console Window App. What I need to do is have a user enter in some integers and press spacebar and enter more numbers and I need to evaluate the numbers one at a time on the fly without the user pressing Enter. I then have to do so other stuff with the numbers but that's not an issue.
Should I use Console.Read() or Console.ReadKey(), I know ReadLine() wont do anything until enter is pressed so wont do what I want.
For your case, ReadKey is more suitable than Read() since the Read terminates when you press the Enter key. But ReadKey() is like below:
The ReadKey method waits, that is, blocks on the thread issuing the ReadKey method, until a character or function key is pressed. A character or function key can be pressed in combination with one or more Alt, Ctrl, or Shift modifier keys. However, pressing a modifier key by itself will not cause the ReadKey method to return.
Hope This will be helpful for you
Difference between Console.Read() and Console.ReadLine()?
As I understand your question you can use Console.ReadLine() instead of using Console.Read()...Give me further details to update more...
I ended up using a ReadKey(), which seemed to work well. Thanks for the help anyway.
numberIntoConsole = Console.ReadKey();
numberReceived = (int)Char.GetNumericValue(numberIntoConsole.KeyChar);
if (numberReceived != -1) //checks to see if a spacebar was pressed
{
if(numberReceived == lastNumberEntered) //checks if numberReceived is equal to lastNumberEntered
{
lastNumberEntered = numberReceived; //make the lastNumberEntered the same as numberReceived
++numberChecker; // add to the numberChecker
} else // else we restart the counter
{
numberChecker = 1;
lastNumberEntered = numberReceived;
}
}

c# Do-While Calling back

I'm stuck in my code!
I have a Do -While Loop and inside I have Switch-Case and each case have If Else Conditions like this :
Here is the problem
Once I press the Retry Button(in If-Else Condition ) I want to go back in middle of the Do Loop and rerun it again for checking...
I'm not quite sure how I should do this.
Any help would be great.
If the switch case is at the bottom of the loop body, you could just call continue and the loop with reiterate from the start.
If you need to start from the middle of the loop, and not the start, the only way I can think of is to wrap the middle in another loop body. Now continue will apply to the inner loop and you will get what you want.
If no other option, you can use goto, but beware the consequences
I really don't know why you'd structure your code like this in a huge loop and switch statement. For me, I'd probably break that up. It would make it simpler to follow. But perhaps you have a reason.
If you can, create an inner loop and continue executing that loop until a non-retry value is returned.
If that really doesn't work, then I'm not above using goto. I know that's taboo for a lot of people but my goal is to write clear code. And if you know what you're doing and a goto results in the clearest code, then I say go for it.
You would probably want to split your code into functions for readability and maintenance piont of view. I would suggest you to try this:
do
{
if(eErrorCode!=0)
{
if( GetResult(eErrorCode) == "Break")
break;
}
}
private string GetResult(eErrorCode)
{
switch(eErrorCode)
...
}

Categories