Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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); 
like image 294
Luke Belbina Avatar asked Dec 15 '10 18:12

Luke Belbina


1 Answers

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.

like image 190
LaGrandMere Avatar answered Oct 01 '22 02:10

LaGrandMere