Windows Console Application Getting Stuck (Needs Key Press) [duplicate]

夙愿已清 提交于 2019-11-26 19:42:17

问题


This question already has an answer here:

  • How and why does QuickEdit mode in Command Prompt freeze applications? 2 answers

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);
}

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.

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.

Also note that sometimes the CPU can really be running at 100% on the machines so maybe thread priority is an issue?

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);

回答1:


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.



来源:https://stackoverflow.com/questions/4453692/windows-console-application-getting-stuck-needs-key-press

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!