C#控制台不停地获取输入(循环) [英] C# Console Take input without pause (in a loop)

查看:514
本文介绍了C#控制台不停地获取输入(循环)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int position1H = 3;
            int position1V = 3;
            int position2H = 75;
            int position2V = 3;

            while (true)
            {

                Console.SetCursorPosition(position1H, position1V);
                Console.BackgroundColor = ConsoleColor.Black;
                Console.WriteLine(" ");
                //take input to move cursor
                //ConsoleKeyInfo KInfo = Console.ReadKey(true);
                //Console.WriteLine(KInfo.Key.ToString());
                
                System.Threading.Thread.Sleep(1);



                Console.SetCursorPosition(position2H, position2V);

            }



这是我代码中的一个循环,不断重复刷新此循环(该程序是一个简单的游戏,用户在其中输入上,下,左,右箭头可在屏幕上移动彩色光标的位置)
我希望它不断刷新循环的控制台,而且还希望在不暂停的情况下从用户那里获取输入..

如果不清楚,请问我将详​​细解释,

但是,有谁知道一种在不暂停用户输入的情况下进行输入的方法...如果您理解我的意思?

就像他们可以在程序不断循环的时候随时输入...



this is a loop from my code where it keeps refreshing this loop over and over again (the program is a simple game where the user enters up, down, left, right arrows to move a position of the coloured cursor around the screen)
i want it to keep refreshing the console with the loop but also take input from the user without pausing it..

if this is unclear just ask i''ll explain in more detail,

but does anyone know a way to take input without pausing for the user to give the input... if you get what i mean?

like they can enter it at anytime while the program keeps looping...

推荐答案

您应该使用System.Console.ReadKey(true)一样,但是在单独的线程中就可以了.由于此方法的参数,此调用将阻塞调用线程,直到按下某个键为止,并且不会显示任何反馈.另一个线程将显示输出而不会暂停.很简单.

我不确定在游戏机上创建游戏是否有意义,但是也许可以做一个有趣的练习.好处之一:它可以通过Mono运行多种不同的操作系统,例如Unix,Mac OS X等,而无需重新编译.

—SA
You should use System.Console.ReadKey(true), but in a separate thread, that''s it. This call will block the calling thread until some key is hit, and no feedback will be shown, thanks to this method''s parameters. Another thread will show output without pause. Pretty simple.

I''m not sure creating a game in a console makes sense, but perhaps it can make an interesting exercise. One benefit: it can be run an many different OS like Unix, Mac OS X and more, through Mono — without recompilation.

—SA


这篇关于C#控制台不停地获取输入(循环)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆