How can I keep a console open until CancelKeyPress event is fired?

前端 未结 5 550
失恋的感觉
失恋的感觉 2021-01-12 00:19

What is the best way to keep a console application open as long as the CancelKeyPress event has not been fired?

I would prefer to not use Console.Read or Console.Rea

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-12 01:06

    There is already a handler bound to CancelKeyPress that terminates your application, the only reason to hook to it is if you want to intercept the event and prevent the app from closing.

    In your situation, just put your app into an infinite loop, and let the built in event handler kill it. You may want to look into using something like Wait(1) or a background process to prevent it from using tons of CPU while doing nothing.

提交回复
热议问题