❔ Processing window messages in while loop in console
How do I process window messages while my code is in a
while(true)
loop with Thread.Sleep()
in console? Because I need my code to run in a loop until user closes the window.11 Replies
Need more context
My app has a
while(running)
loop and running
bool controls that while loop, and I need my app to process window messages (such as user clicking X on window, or Ctrl+C to exit) in that loop
So basically I need some way to set that running
bool to false when user wants to close the window.Can you paste code of what you've done
?
I know there's a
Form.FormClosing
event on WinForms, but is there a similar one for console appsConsole.CancelKeyPress
Yes @nolemretaW you can use Console.CancelKeyPress
In main you can write
Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);
Okay, thank you, it works
you don't need to explicitly instantiate a delegate like that
just
Console.CancelKeyPress += MyHandlerMethod
Ohh something new to learn, thanks.
i think you used to have to, so a lot of old documentation for winforms and stuff shows it that way
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.