Click here to Skip to main content
13,143,774 members (31,064 online)
Rate this:
Please Sign up or sign in to vote.
See more:
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.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 18-Dec-11 18:42pm
Updated 18-Dec-11 18:43pm
Abhinav S415.8K
Abhinav S 19-Dec-11 0:43am
Pre tags added.
SAKryukov 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?

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

amitgajjar 19-Dec-11 1:31am
my 5 for you :)
SAKryukov 19-Dec-11 1:35am
Thank you,
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?
SAKryukov 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?
Joel Whatley- 19-Dec-11 1:45am
Yes it's solved :P thx anyway -
SAKryukov 19-Dec-11 1:47am
Great. You are welcome.
Good luck, call again.
RaisKazi 19-Dec-11 3:03am
My 5.
SAKryukov 19-Dec-11 3:06am
Thank you, Rais.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170915.1 | Last Updated 19 Dec 2011
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100