Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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...
Posted
Updated 18-Dec-11 18:43pm
v2
Comments
Abhinav S 19-Dec-11 0:43am    
Pre tags added.
Sergey Alexandrovich Kryukov 19-Dec-11 1:13am    
Oh, why Sleep(1)? why not Sleep(0), not Sleep(300)? Every time you hard-code some concrete value, you can be sure it's wrong. Got the idea?
--SA

1 solution

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
 
Share this answer
 
v3
Comments
AmitGajjar 19-Dec-11 1:31am    
my 5 for you :)
Sergey Alexandrovich Kryukov 19-Dec-11 1:35am    
Thank you,
--SA
Joel Whatley- 19-Dec-11 1:32am    
Thanks :) and it turned out I didn't even need anything extra, the loop pauses to accept input (for example WASD for player1 and Up,Down,left,right arrow for player2) but nothing changes on the screen if no input is given, so it doesn't even matter :P and yeah i'm just practising making anything in the console.. new to this :D
you want the code to see what i mean?
Sergey Alexandrovich Kryukov 19-Dec-11 1:38am    
No, I see what you mean. If nothing should change if the user does not hit a key, you just wasted my time. You could use ReadKey and have your functionality like in a regular interactive console application. But you can use my method if you do anything in parallel.

So, is your problem solved? If so, please accept the answer formally (green button).
If not, (sigh), well, what's the problem?
--SA
Joel Whatley- 19-Dec-11 1:45am    
Yes it's solved :P thx anyway -

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900