Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# CURSOR Console
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 18-Dec-11 19:42pm
Edited 18-Dec-11 19:43pm
Abhinav S380.3K
v2
Comments
Abhinav S at 19-Dec-11 0:43am
   
Pre tags added.
SAKryukov at 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

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

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

  Print Answers RSS
0 OriginalGriff 250
1 George Jonsson 175
2 Jochen Arndt 150
3 Kornfeld Eliyahu Peter 149
4 PIEBALDconsult 110
0 OriginalGriff 6,080
1 DamithSL 4,648
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,624
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 19 Dec 2011
Copyright © CodeProject, 1999-2014
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