C# - 在控制台应用程序执行期间捕获空格键按下
有没有办法在控制台应用程序中运行进程,并且在执行过程中,如果按下空格键,则用应用程序的状态更新控制台?我们有一个解析文件进行格式化的进程,在执行过程中,状态不会更新。有没有一种方法可以在执行过程中捕获键盘事件,类似于 CTRL-C 委托方法?
TL/DR:在运行过程中,使用空格键更新屏幕状态。
C# 控制台应用程序。
Is there a way to run a process in a console application, and during the execution, if the space bar is pressed, update the console with the status of the application? We have a process that parses files for formatting, and during execution, the status is not updated. Is there a way to capture the keyboard event during execution similar to the CTRL-C delegate approach?
TL/DR: During a running process, use the spacebar to update the screen with status.
C# Console application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然可以,但是您需要一个后台线程来进行实际处理。基本上,只需让控制台进程在后台线程中启动文件解析,然后在其工作时循环检查按键和 Thread.Yield() 语句。如果按下某个键,则从后台线程工作时正在更新的某个类获取状态更新:
Well sure, but you'll need a background thread for the actual processing. Basically, just have your console process kick off your file parsing in a background thread, then while it's working, loop through a check for a keypress and a Thread.Yield() statement. If a key was pressed, get a status update from some class the background thread is updating as it works: