C#不完整的Console.ReadKey()调用我不希望用户完成

时间:2019-06-23 00:24:58

标签: c# timeout console.readkey

我正在学习套接字消息。我在while循环中中断了Console.ReadKey(),并且许多调用最终都未完成。我试图找到一种方法来删除未完成的呼叫,而无需用户全部输入。

我见过

while(Console.KeyAvailable){Console.ReadKey(true);}

但是我遇到了相反的问题,即太多的电话和不足的击键。

How to add a Timeout to Console.ReadLine()? 这个问题使我到达了现在的位置,但并不能解决我目前的问题。

using System;
using System.Threading;

class Program
{
    static void Main(string[] args)
    {
        DontLockOnCharGet bug = new DontLockOnCharGet();
        bug.Setup();
    }
}

public class DontLockOnCharGet
{
    public void Setup()
    {
        while (true) { DontLockCharGet(); }
    }

    public void DontLockCharGet()
    {
        while (true)
        {
            // used to interrupt the Console.ReadKey() function
            AutoResetEvent getInput = new AutoResetEvent(false);
            AutoResetEvent gotInput = new AutoResetEvent(false);

            // Console.ReadKey() assigns to input
            char input = ' ';

            //Lambda used to get rid of extra class
            Thread tom = new Thread(() =>
            {
                getInput.WaitOne(); // Waits for getInput.Set()

                //The problem with this is the read keys stacking up
                // causing the need for a lot of keystrokes
                input = Console.ReadKey().KeyChar;

                gotInput.Set();
            })
            {
                IsBackground = true
            };
            // Starts Lambda function
            tom.Start(); 

            // Allows thread to pass WaitOne() in Lambda
            getInput.Set(); 
            // Gives some milliseconds for before stopping Lambda exe
            gotInput.WaitOne(2000);

            if (input == 'S' || input == 's')
            {
                break;
            }
            // thinking I would put the solution here
            //...
        }
        //Do stuff if input is s || S
        Console.Write("end: ");
    }
}

我希望能够按's'|| 'S',然后键入一条消息,但是根据我等待了多长时间,我可能必须长时间按住's'。

由于第一个评论,我遇到的解决方案。

using System;
using System.Threading;

/// <summary>
/// Problem fixed I don't know why
/// Probably not making a new function for each call
/// </summary>
class Program
{
    static void Main(string[] args)
    {
        DontLockOnCharGet bug = new DontLockOnCharGet();
        bug.Setup();
    }
}
public class DontLockOnCharGet
{
    public void Setup()
    {
        while (true) { DontLockCharGet(); }
    }

    public void DontLockCharGet()
    {
        while (true)
        {
            //Specifies time to wait for input
            char i = Reader.ReadKey(1000);
            if (i == 's' || i == 'S')
            {
                //Do stuff if input is s || S
                break;
            }
            Console.Write(i);
        }
        // Do stuff
        Console.Write("end: ");
    }
}

class Reader
{
    private static Thread inputThread;
    private static AutoResetEvent getInput, gotInput;
    private static char input;

    static Reader()
    {
        //Setup once
        getInput = new AutoResetEvent(false);
        gotInput = new AutoResetEvent(false);

        //inputThread = new Thread(reader);
        //inputThread.IsBackground = true;
        //inputThread.Start();
    }

    private static void reader()
    {
        //waits for .Set()
        getInput.WaitOne();
        input = '\0';
        input = Console.ReadKey().KeyChar;
        //Marks if input is gotten
        gotInput.Set();
    }

    // omit the parameter to read a line without a timeout
    public static char ReadKey(int timeOutMillisecs = Timeout.Infinite)
    {
        //Setup and start read thread
        inputThread = new Thread(reader)
        {
            IsBackground = true
        };
        inputThread.Start();
        //Allows thread to continue in reader()
        getInput.Set();
        //Wait for input or back out befor it is given
        bool success = gotInput.WaitOne(timeOutMillisecs);
        return input;
    }
}

此版本的代码按预期工作: 输入“ S”,它会自动完成为“发送:”

1 个答案:

答案 0 :(得分:0)

问题出在AMDAPP中,这是在创建一个新函数,而不仅仅是一个新函数调用。应该将正在创建的函数移到这样的单独函数中

new Thread(()=> { ... });

在班级内部。

做这些

private void ReadKey(){
        // Waits for getInput.Set()
        getInput.WaitOne();

        //The problem with this is the read keys stacking up
        // causing the need for a lot of keystrokes
        input = Console.ReadKey().KeyChar;

        gotInput.Set();
}

对变量进行分类并在AutoResetEvent getInput, gotInput; char input; 内对其进行初始化

最后调用Setup(){...}当前正在创建新功能的地方。

注意:此答案不是最佳实践,但将使原型可用。