I've implememted a small Console Application that checks how long a programm is running. I then tried to run the application and everything is working fine. However then i hit (by accident) the "pause" key on my Keyboard and the programm stopped executing.
Is there a way to handle this event in a Console Application to suppress this pausing?
Update:
class Program
{
static void Main(string[] args)
{
while (true)
{
Thread.Sleep(2000);
var p = Process.GetProcessesByName("wineks");
if (p != null)
{
Console.WriteLine("Found Process. Close it please");
Console.ReadKey();
}
}
}
}
That is basically my code. It is only asking the user to close a specific process. If I know hit the Pause Button on my Keyboard before I see the message, the message will never appear because the application freezes and seems like paused.
From browsing in the Internet I know that the key I press has the Name Pause and the key is sending some kind of Event or Signal to the Console.
Actually the Console does not have a way to raise KeyPress events , you can however try some looped approach to handle any key press done accidentally . Refer to this stackoverflow question here
Try inserting these lines before GetProcessByName
if (Console.KeyAvailable)
{
if (ConsoleKey.Pause == Console.ReadKey().Key)
continue;
}
i made an app and sometimes it just pauses until I press a key.
i do not use any code which requiereres any input or interaction (such as readline or readkey).
this randomly happens like 1 in 20 times after Thread.Sleep and after you press any key it continues to work perfectly.
if (Convert.ToInt64(timetotask[0]) > 0)
{
Thread.Sleep(Convert.ToInt32(timetotask[0]));
}
else
{
mylog.log("Task was in the past, executing it now");
}
int currentbid = placebid(task.itemid, bid, driver[Convert.ToInt32(task.account)]);
the first line of placebid is console.writeline("mystring") so i do not see anything what could cause this behaviour.
Does it have something to do with debug mode?
thanks for help
This could be obvious but if pressing a key in the console makes the application resume, it is most likely you managed to pause the app via the console by pressing a key.
When it is sleeping do you ever press a key to check if it has paused? Then eventually the sleep ends as it matches the rule and then when you next press a key it unpauses the program...
Michael
I am writing a console app and found this method to end a loop on a keypress:
while (!Console.KeyAvailable){//do stuff}
It works, but it echos the key that was pressed back to the prompt. Is there a better method?
edit:
To clarify more, the loop runs and if hit the letter j the loop ends and the program exits. However, I get the following output at the prompt:
C:\>j
If you want to exit from a loop after a particular key is pressed, then instead of Console.KeyAvailable you can use the Console.ReadKey() function and check the return type of this function as an exit condition.
Here is the implementation.
while (true) {
var keyPressed = Console.ReadKey(true);
if (keyPressed.KeyChar == 'j') break;
//do something
Console.WriteLine("Key pressed: " + keyPressed.KeyChar);
}
For more details: C# Console - hide the input from console window while typing
I'm writing an console application witch Displaying certain data on the console screen, than checking for user input from the keyboard and finally handleing it by need. all single threaded.
For that i tried using Keyboard.IsKeyDown Method from System.Windows.Input namespace. and visual studio wo'nt allow it.
Does anyone knows why and can help me?
I dont see other way implementing that logic using only one thread and no timer's.
Use Console.ReadKey() to read input from the keyboard in a console application.
Note that this is a blocking call. If you don't want to block, combine with Console.KeyAvailable. For example, this program will loop and display if a key is pressed every 10th of a second:
static void Main(string[] args)
{
do
{
if (Console.KeyAvailable)
{
var key = Console.ReadKey();
Console.WriteLine(key.Key);
}
else
{
Console.WriteLine("No key pressed");
}
System.Threading.Thread.Sleep(100);
} while (true);
}
When I run my program, the console window seems to run and close. How to keep it open so I can see the results?
class Program
{
public class StringAddString
{
public virtual void AddString()
{
var strings2 = new string[] { "1", "2", "3", "4", "5","6", "7", "8", "9"};
Console.WriteLine(strings2);
Console.ReadLine();
}
}
static void Main(string[] args)
{
StringAddString s = new StringAddString();
}
}
Put a Console.Read() as the last line in your program. That will prevent it from closing until you press a key
static void Main(string[] args)
{
StringAddString s = new StringAddString();
Console.Read();
}
If you want to keep it open when you are debugging, but still let it close normally when not debugging, you can do something like this:
if (System.Diagnostics.Debugger.IsAttached) Console.ReadLine();
Like other answers have stated, the call to Console.ReadLine() will keep the window open until enter is pressed, but Console.ReadLine() will only be called if the debugger is attached.
There are two ways I know of
1) Console.ReadLine() at the end of the program. Disadvantage, you have to change your code and have to remember to take it out
2) Run outside of the debugger CONTROL-F5 this opens a console window outside of visual studio and that window won't close when finished. Advantage, you don't have to change your code. Disadvantage, if there is an exception, it won't drop into the debugger (however when you do get exceptions, you can simply just rerun it in the debugger)
Console.ReadKey(true);
This command is a bit nicer than readline which passes only when you hit enter, and the true parameter also hides the ugly flashing cursor while reading the result :) then any keystroke terminates
You forgot calling your method:
static void Main(string[] args)
{
StringAddString s = new StringAddString();
s.AddString();
}
it should stop your console, but the result might not be what you expected, you should change your code a little bit:
Console.WriteLine(string.Join(",", strings2));
You can handle this without requiring a user input.
Step 1. Create a ManualRestEvent at the start of Main thread
ManualResetEvent manualResetEvent = new ManualResetEvent(false);
Step 2 . Wait ManualResetEvent
manualResetEvent.WaitOne();
Step 3.To Stop
manualResetEvent.Set();
Write Console.ReadKey(); in the last line of main() method. This line prevents finishing the console. I hope it would help you.
If your using Visual Studio just run the application with Crtl + F5 instead of F5. This will leave the console open when it's finished executing.
Use Console.Readline() at the end .Your code will not close until you close it manually.Since Readline waits for input that needs to be entered for your code hence your console will be open until you type some input.
For visual c# console Application use:
Console.ReadLine();
Console.Read();
Console.ReadKey(true);
for visual c++ win32 console application use:
system("pause");
press ctrl+f5 to run the application.
Make sure to useConsole.ReadLine();
to keep the preceeding Console.WriteLine(""); message from closing.
Console.Read()
-> Console stays open until you press a button on your keyboard
To be able to give it input without it closing as well you could enclose the code in a while loop
while (true)
{
<INSERT CODE HERE>
}
It will continue to halt at Console.ReadLine();, then do another loop when you input something.
If you're using Visual Studio, then the IDE has an option to keep the window open under
Tools > Options > Debugging >
Automatically close the console when debugging stops
Unlike CTRL + F5, this allows you to use breakpoints while debugging.