C# - 在控制台应用程序执行期间捕获空格键按下

发布于 2024-10-19 20:54:38 字数 184 浏览 0 评论 0原文

有没有办法在控制台应用程序中运行进程,并且在执行过程中,如果按下空格键,则用应用程序的状态更新控制台?我们有一个解析文件进行格式化的进程,在执行过程中,状态不会更新。有没有一种方法可以在执行过程中捕获键盘事件,类似于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦过后 2024-10-26 20:54:38

当然可以,但是您需要一个后台线程来进行实际处理。基本上,只需让控制台进程在后台线程中启动文件解析,然后在其工作时循环检查按键和 Thread.Yield() 语句。如果按下某个键,则从后台线程工作时正在更新的某个类获取状态更新:

private static StatusObject Status;

public static void main(params string[] args)
{
   var thread = new Thread(PerformProcessing);
   Status = new StatusObject();
   thread.Start(Status);

   while(thread.IsAlive)
   {
      if(keyAvailable)
         if(Console.ReadKey() == ' ')
            ShowStatus(Status);

      //This is necessary to ensure that this main thread doesn't monopolize
      //the CPU going through this loop; let the background thread work a while
      Thread.Yield();
   }

   thread.Join();
}

public void PerformProcessing(StatusObject status)
{
   //do your file parsing, and at significant stages of the process (files, lines, etc)
   //update the StatusObject with vital info. You will need to obtain a lock.
}

public static void ShowStatus(StatusObject status)
{
   //lock the StatusObject, get the information from it, and show it in the console.
}

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:

private static StatusObject Status;

public static void main(params string[] args)
{
   var thread = new Thread(PerformProcessing);
   Status = new StatusObject();
   thread.Start(Status);

   while(thread.IsAlive)
   {
      if(keyAvailable)
         if(Console.ReadKey() == ' ')
            ShowStatus(Status);

      //This is necessary to ensure that this main thread doesn't monopolize
      //the CPU going through this loop; let the background thread work a while
      Thread.Yield();
   }

   thread.Join();
}

public void PerformProcessing(StatusObject status)
{
   //do your file parsing, and at significant stages of the process (files, lines, etc)
   //update the StatusObject with vital info. You will need to obtain a lock.
}

public static void ShowStatus(StatusObject status)
{
   //lock the StatusObject, get the information from it, and show it in the console.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文