第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

c# 不完整的 Console.ReadKey() 調(diào)用我不希望用戶完成

c# 不完整的 Console.ReadKey() 調(diào)用我不希望用戶完成

C#
倚天杖 2023-04-29 09:55:22
我正在學(xué)習(xí)套接字消息傳遞。我在 while 循環(huán)中中斷了 Console.ReadKey() 并且很多調(diào)用最終都未完成。我正在嘗試找到一種方法來(lái)刪除不完整的調(diào)用,而無(wú)需用戶將其全部輸入。我見(jiàn)過(guò)while(Console.KeyAvailable){Console.ReadKey(true);}但我有相反的問(wèn)題,太多的電話沒(méi)有足夠的擊鍵。如何向 Console.ReadLine() 添加超時(shí)? 這個(gè)問(wèn)題讓我到了現(xiàn)在的位置,但它并沒(méi)有解決我當(dāng)前的問(wèn)題。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' 然后輸入一條消息,但根據(jù)我等待的時(shí)間長(zhǎng)短,我可能需要按住 's' 很長(zhǎng)時(shí)間。
查看完整描述

1 回答

?
翻閱古今

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超5個(gè)贊

問(wèn)題出在new Thread(()=> { ... });This is creating a new function 而不僅僅是新函數(shù)調(diào)用。正在創(chuàng)建的函數(shù)應(yīng)該像這樣移動(dòng)到一個(gè)單獨(dú)的函數(shù)中


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();

}

在班級(jí)里面。


做這些


AutoResetEvent getInput, gotInput;

char input;

類變量并在內(nèi)部初始化它們Setup(){...}


最后Thread tom = new Thread(ReadKey);在當(dāng)前正在制作新功能的地方調(diào)用。


注意:此答案不適用于最佳實(shí)踐,但會(huì)得到一個(gè)原型來(lái)工作。


查看完整回答
反對(duì) 回復(fù) 2023-04-29
  • 1 回答
  • 0 關(guān)注
  • 121 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)