I have a cmd application when i pressed insert while aplication is maximized run normal
but when application is minimized not work
What could I do
please help me
using System;
using System.Runtime.InteropServices;
class MainClass
{
static void Main()
{
ConsoleKeyInfo keypress;
keypress = Console.ReadKey(); // read keystrokes
if (keypress.Key == ConsoleKey.Insert)
{
Console.Write("One ");
}
}
}
not recognized key insert while is minimized
I think, you are looking for Global Hooks.
How can it work when the command line is minimized? It doesn't have any focus and your key strokes aren't registered.
The same can be said for almost any program - try it with notepad for instance
Edit: if you want to register to global key events, use glob hooks like TcKs suggested
I am trying to update a C# application published by VS2010 on IIS7.5.
I need to uninstall it and then install a new one.
If I ran the code in debug mode, it worked well.
But, if I ran it in release without debug mode. I got error of
Another version of the product is already installed. Installation of this version cannot continue. To configure or remove the existing version of this product, use Add/remove programs on the control panel.
I found the reason is that "install" started when "uninstall" is still not finished. How to make sure that the "uninstall" is finished before "install" started ? I used WaitForExit() (https://msdn.microsoft.com/en-us/library/ty0d8k56.aspx) but, after uninstall, the "install" was not executed.
The code is here:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace msiexec_uninstall_install
{
class Program
{
static void Main(string[] args)
{
var p = System.Diagnostics.Process.Start("msiexec", "/uninstall http://MyServer/MyApp.msi");
p.WaitForExit();
Console.WriteLine("after uninstall");
System.Diagnostics.Process.Start("msiexec", "/i http://myServer/MyApp_new.msi");
Console.WriteLine("after install");
Console.ReadLine();
}
}
}
If your installation takes longer than it takes to finish your Main program, you won't see it.
Try adding WaitForExit(); for your installation as well.
static void Main(string[] args)
{
var uninstallProcess = System.Diagnostics.Process.Start("msiexec", "/uninstall http://MyServer/MyApp.msi");
uninstallProcess.WaitForExit();
Console.WriteLine("after uninstall");
var installProcess = System.Diagnostics.Process.Start("msiexec", "/i http://myServer/MyApp_new.msi");
installProcess.WaitForExit();
Console.WriteLine("after install");
Console.ReadLine();
}
That way your Main method will only end after installation is complete. To be on the safe side, you can even add some timeout for WaitForExit():
installProcess.WaitForExit(10000); //10 sec
This question already has answers here:
Why is the console window closing immediately once displayed my output?
(15 answers)
Closed 7 years ago.
I'm learning to program in C# and I'm having problems with the console, when I run the code below the console show the output but immediately close the window and I can't see anything. I don't know what to do to keep the window open. Any suggestion? I'll be very grateful.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testApp_1
{
class Program
{
static void Main(string[] args)
{
String word = "Hello world!";
Console.WriteLine(word);
}
}
}
This happens because when you reach the last line, you also reach the end of the program, thus it terminates. To keep the console window open, just add
Console.ReadKey() as the last line.
The execution continues after writing to the console and then the program exits. you need to do something to pause the execution, usually you would read something from the console:
Console.ReadKey();
to pause until the user presses a key
so your whole program might look like:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testApp_1
{
class Program
{
static void Main(string[] args)
{
String word = "Hello world!";
Console.WriteLine(word);
//if you want the user to exit with any key press do this
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
//if you want the user to hit 'enter' to exit do this
Console.WriteLine("Press enter to exit.");
Console.ReadLine();
}
}
}
you can read about Console.ReadKey and Console.ReadLine on MSDN
Your code closes immediately, because it doesn't have anything else to do. What you'll want to do is to add this to the bottom of your code:
Console.Readline();
That will cause it to wait until you press Enter.
You can use following method to read some user input:
https://msdn.microsoft.com/de-de/library/system.console.readline%28v=vs.110%29.aspx
But you have to do some input.
You can also use logger frameworks: log4net:
http://logging.apache.org/log4net/
Console.ReadLine() is missing.
If you hit ctrl + F5 ( run without debugging) it will not close. But if you run with debugging, it will close after execution.
You can use Console.ReadLine(); in the end to wait for the user to hit return.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace testApp_1
{
class Program
{
static void Main(string[] args)
{
String word = "Hello world!";
Console.WriteLine(word);
Console.ReadLine();
}
}
}
I'm trying to run a program like a for loop. But when it starts debugging the console window disappears immediately. How do I stop this. I need something like press any key to continue.
Have a statement like
Console.Read()
That way, the console will remain until you press a key.
For more information, read here.
Write Console.ReadLine() or Console.Read() or Console.ReadKey() at the end of your program.
It will make your screen wait for your key press in order to exit.
I just found out that pressing the Ctrl + F5 key makes the console stay.
You didn't mention any example code. This is my example code. Program waits for pressing a key
class Program
{
static void Main(string[] args)
{
for (int i = 0; i < 3; i++)
{
Console.WriteLine(i);
}
Console.ReadKey();
}
}
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.