问题描述
我有一个控制台程序,它有不同的组件,运行方式如下:
I have a console program that has different components that run like this:
void start() {
while(true){
DoSomething();
Thread.Sleep(1000*5);
}
}
我的主要入口点看起来像[伪代码]
My main entry point looks like [pseudo-ish code]
Thread.Start(Componenet1.Start);
Thread.Start(Componenet2.Start);
while(true){
Console.Writeline("running");
Thread.Sleep(1000*5);
}
任何地方都没有 Console.Reads.我的问题是有时应用程序运行良好,但随后停止,如果我按窗口上的任意键,它将再次开始工作.这种情况很少发生,但我将此程序部署在 100 多个虚拟机上,这些虚拟机在自动化环境中 24/7 全天候运行.
There are no Console.Reads anywhere. My problem is SOMETIMES the application will be running great but then stop and if I press any key on the window it will start working again. This happens fairly infrequently but I have this program deployed on 100+ VM's running 24/7 in an automated environment.
同样在计算机上,我有一些 AHK 脚本和其他操作鼠标的东西,但不确定是否与它有关.
Also on the computer I have some AHK scripts and other stuff that manipulate the mouse but not sure if that has anything to do with it.
另请注意,有时 CPU 真的可以在机器上以 100% 的速度运行,所以线程优先级可能是一个问题?
Also note that sometimes the CPU can really be running at 100% on the machines so maybe thread priority is an issue?
解决方案:您需要禁用快速编辑模式.这是执行此操作的 C# 代码:
SOLUTION: You need to disable quick edit mode. Here is working C# code to do this:
// http://msdn.microsoft.com/en-us/library/ms686033(VS.85).aspx
[DllImport("kernel32.dll")]
public static extern bool SetConsoleMode(IntPtr hConsoleHandle, uint dwMode);
private const uint ENABLE_EXTENDED_FLAGS = 0x0080;
static void Main(string[] args)
{
IntPtr handle = Process.GetCurrentProcess().MainWindowHandle;
SetConsoleMode(handle, ENABLE_EXTENDED_FLAGS);
推荐答案
如果用户不小心点击进入黑色控制台窗口,光标会变成一个填充的白色矩形,应用程序会挂在下一个 Console.Write 语句,直到又一次点击.
If the user accidentally clicks into the black console window, the cursor changes to a filled white rectangle, and the app hangs at the next Console.Write statement, until another clic is made.
启用快速编辑模式"后,这是控制台窗口的通用功能.
It is a generic feature of the Console window when its "QuickEdit Mode" is enabled.
为了禁用该功能,您应该在运行时取消选中应用控制台窗口的快速编辑模式"选项.
In order to disable that feature, you should uncheck the "QuickEdit Mode" option of your app's console window at run-time.
这篇关于Windows 控制台应用程序卡住(需要按键)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!