0

我正在学习套接字消息传递。我在一个while循环中打断了 Console.ReadKey() ,很多调用都以不完整的形式结束。我正在尝试找到一种方法来删除不完整的呼叫,而无需用户全部输入。

我见过

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

但我有相反的问题,电话太多,按键次数不够。

如何向 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”并自动完成“发送:”

4

1 回答 1

0

问题在于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(){...}

最后调用Thread tom = new Thread(ReadKey);当前正在创建新函数的位置。

注意:此答案不适用于最佳实践,但可以让原型工作。

于 2019-06-25T00:26:46.253 回答