c# - 控制台应用程序中的消息泵

标签 c# .net console-application message-pump

我有一个用 .NET 编写的相当简单的控制台应用程序。有时,应用程序在没有运算符(operator)的情况下以批处理模式运行,有时则“自掏腰包”运行。如果它以批处理模式运行,则有一个定义的默认选项允许程序自动运行。如果有运算符(operator)在场,还有其他选项允许用户从功能列表中进行选择。

由于我不想深入的原因,命令行参数不是首选。相反,我创建了一个 10 秒的窗口,运算符(operator)可以在其中选择一个功能。目前,我正在使用一个简单的 while 循环并从“输入”流中读取输入。我添加了一个 Thread.Sleep在最后调用以防止 while 循环完全消耗处理器,但我想知道是否有更好的方法。

在 Windows 应用程序(Windows FormsWPF)中,有一个消息泵能够读取消息队列,然后将控制权返回给系统。甚至像 Visual Studio 这样的重型应用程序,SAS Enterprise GuideSQL Server Management Studio空闲时几乎使用 0% 的处理器。我可以使用我的控制台应用程序获得相同的效果吗?

Thread.Sleep 正在运行,但正如我所说,我想知道是否有更好的方法。

这是源代码:

class Program {
    static void Main( string[] args ) {

        DateTime l_startTime = DateTime.Now;

        Console.CursorVisible = false;
        Console.WriteLine( "Please select an option within 10 seconds..." );
        Console.WriteLine( "" );
        Console.WriteLine( " [1] Option A (DEFAULT)" );
        Console.WriteLine( " [2] Option 2" );
        Console.WriteLine( " [3] Option III" );

        int l_elapsedSeconds = 0;

        bool l_exit = false;
        while ( !l_exit ) {

            int l_currentElapsedSeconds = (int) Math.Floor( ( DateTime.Now - l_startTime ).TotalSeconds );
            if ( l_currentElapsedSeconds > l_elapsedSeconds ) {
                Console.CursorTop = 0;
                Console.CursorLeft = 0;
                l_elapsedSeconds = l_currentElapsedSeconds;

                int l_remainingSeconds = 10 - l_elapsedSeconds;

                Console.WriteLine( String.Format( "{0,-80}", "Please select an option within " + l_remainingSeconds + " seconds..." ) );
            }

            if ( l_elapsedSeconds >= 10 ) {
                OptionA();
                break;
            }

            if ( Console.KeyAvailable ) {
                var l_key = Console.ReadKey( true );

                switch ( l_key.Key ) {
                    case ConsoleKey.D1:
                        OptionA();
                        l_exit = true;
                        break;

                    case ConsoleKey.D2:
                        Option2();
                        l_exit = true;
                        break;

                    case ConsoleKey.D3:
                        OptionIII();
                        l_exit = true;
                        break;
                }
            }

            if ( !l_exit )
                // Don't eat all the processor
                System.Threading.Thread.Sleep( 100);
        }

        Console.CursorTop = 7;
        Console.CursorLeft = 0;

        Console.Write( "Press any key to continue...");
        Console.ReadKey( true);

    }

    static void OptionA() {
        Console.CursorTop = 6;
        Console.CursorLeft = 0;

        Console.WriteLine( "Option A Selected!");
    }

    static void Option2() {
        Console.CursorTop = 6;
        Console.CursorLeft = 0;

        Console.WriteLine( "Option 2 Selected!");
    }

    static void OptionIII() {
        Console.CursorTop = 6;
        Console.CursorLeft = 0;

        Console.WriteLine( "Option III Selected!");
    }
}

注意:这个问题与超时无关...它是关于在等待响应时使用 0% 的处理器时间(如窗口应用程序)。

最佳答案

您可以启动一个在后台读取按键的线程。将键添加到阻塞队列并在主线程中等待队列被填充,例如

var queue = new BlockingCollection<ConsoleKeyInfo>();

new Thread(() =>
{
    while (true) queue.Add(Console.ReadKey(true));
})
{ IsBackground = true }.Start();


Console.Write("Welcome! Please press a key: ");

ConsoleKeyInfo cki;

if (queue.TryTake(out cki, TimeSpan.FromSeconds(10))) //wait for up to 10 seconds
{
    Console.WriteLine();
    Console.WriteLine("You pressed '{0}'", cki.Key);
}
else
{
    Console.WriteLine();
    Console.WriteLine("You did not press a key");
}

后台线程和主线程在分别等待 ReadKey 和 TryTake 返回时都将休眠(使用 0% 处理器时间)。

关于c# - 控制台应用程序中的消息泵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12221626/

相关文章:

c# - 填充字符出现时的解密问题

c# - 在 SignalR 上发送/接收复杂对象

c# - 将 MenuItem 添加到控制台应用程序中 TrayIcon 的上下文菜单

c++ - 如何在控制台应用程序中添加滚动功能以显示更大的输入?

C# - 在 if 语句中使用正则表达式

c# - 存储上次应用程序运行的日期/时间

c# - C# 是否清理 C++ 分配的内存?

c# - 如何在两个不同的选项卡上显示相同的控件?

c# - 用于获取刷新 token 的 Google C# 客户端库

c# - 如何在 RestSharp 中使用 ExecuteAsync 返回变量