Console Check
-
I have a console app in C# that runs for a very long time. Basically, it loops through from one to a brazillian for days on end. Depending on conditions met, it will print certain output. Nothing unusual there. Is it possible to press a key and have it tell me which loop index I'm in? I do not want to programmatically print the index every Nth time or second. I would rather have it wait until I press a key and have it spit it out. I guess from a threaded Windows app, this would be easier. I'm trying to see if there's a way to do it from a command-line app on-demand, without having to do it iteratively.
-
I have a console app in C# that runs for a very long time. Basically, it loops through from one to a brazillian for days on end. Depending on conditions met, it will print certain output. Nothing unusual there. Is it possible to press a key and have it tell me which loop index I'm in? I do not want to programmatically print the index every Nth time or second. I would rather have it wait until I press a key and have it spit it out. I guess from a threaded Windows app, this would be easier. I'm trying to see if there's a way to do it from a command-line app on-demand, without having to do it iteratively.
You could always use Console.KeyAvailable to do this. This is a none blocking method, so you could use that to output the variable, e.g.
if (Console.KeyAvailable)
Console.WriteLine("Currently at index {0}", index);I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier -
I have a console app in C# that runs for a very long time. Basically, it loops through from one to a brazillian for days on end. Depending on conditions met, it will print certain output. Nothing unusual there. Is it possible to press a key and have it tell me which loop index I'm in? I do not want to programmatically print the index every Nth time or second. I would rather have it wait until I press a key and have it spit it out. I guess from a threaded Windows app, this would be easier. I'm trying to see if there's a way to do it from a command-line app on-demand, without having to do it iteratively.
Hi, Try this:
static int index;
static void Main(string[] args)
{
Console.WriteLine("Press the I key to get loop index");
System.Threading.Thread thr = new System.Threading.Thread(ReadKey);
thr.Start();
for (index = 0; index < 10000; index++)
{
// some code
System.Threading.Thread.Sleep(5); // I added this line to simulate long task
}
Console.WriteLine("Press the I key to continue . . .");
}static void ReadKey()
{
while (index < 10000)
{
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
if (keyInfo.Key == ConsoleKey.I && index < 10000)
{
Console.WriteLine(index);
}
}}
Hope this helps.
The quick red ProgramFOX jumps right over the
Lazy<Dog>
. -
You could always use Console.KeyAvailable to do this. This is a none blocking method, so you could use that to output the variable, e.g.
if (Console.KeyAvailable)
Console.WriteLine("Currently at index {0}", index);I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easierPerfect, almost! I had to add two more lines arround the WriteLine command:
Console.Write("\b");
and
Console.ReadKey(false);
The former deletes (backspaces) the character entered, while the latter stops printing a million times. I could also only accept ENTER for printing. :thumbsup:
-
Perfect, almost! I had to add two more lines arround the WriteLine command:
Console.Write("\b");
and
Console.ReadKey(false);
The former deletes (backspaces) the character entered, while the latter stops printing a million times. I could also only accept ENTER for printing. :thumbsup:
Rather than wring a backspace to the console, which I think can be problematic in certain situations, you could use the ReadKey(true) function:
if (Console.KeyAvailable)
{
// Flush the input buffer, suppressing output(echo) of the input
while (Console.KeyAvailble) Console.ReadKey(true);
// TODO: Print the index here, for instance something like
Console.WriteLine("At index {0}", index);
} -
Rather than wring a backspace to the console, which I think can be problematic in certain situations, you could use the ReadKey(true) function:
if (Console.KeyAvailable)
{
// Flush the input buffer, suppressing output(echo) of the input
while (Console.KeyAvailble) Console.ReadKey(true);
// TODO: Print the index here, for instance something like
Console.WriteLine("At index {0}", index);
}