Asynchronous Windows Console input whilst outputting











up vote
1
down vote

favorite












I'm having issues trying to read input whilst outputting at the same time. I need a server console for my game which can receive input whilst outputting and not mess up the buffer.



For example, I'm typing "Hello world" and in the process, player deaths, kills, etc. are being outputted into the console, which would result in something like:




Hello *Player killed Player2*world




Thanks in advance










share|improve this question




























    up vote
    1
    down vote

    favorite












    I'm having issues trying to read input whilst outputting at the same time. I need a server console for my game which can receive input whilst outputting and not mess up the buffer.



    For example, I'm typing "Hello world" and in the process, player deaths, kills, etc. are being outputted into the console, which would result in something like:




    Hello *Player killed Player2*world




    Thanks in advance










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm having issues trying to read input whilst outputting at the same time. I need a server console for my game which can receive input whilst outputting and not mess up the buffer.



      For example, I'm typing "Hello world" and in the process, player deaths, kills, etc. are being outputted into the console, which would result in something like:




      Hello *Player killed Player2*world




      Thanks in advance










      share|improve this question















      I'm having issues trying to read input whilst outputting at the same time. I need a server console for my game which can receive input whilst outputting and not mess up the buffer.



      For example, I'm typing "Hello world" and in the process, player deaths, kills, etc. are being outputted into the console, which would result in something like:




      Hello *Player killed Player2*world




      Thanks in advance







      c++ windows winapi console






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 19:33









      anothermh

      2,80411226




      2,80411226










      asked Jun 6 '09 at 15:38









      Saul

      662923




      662923
























          4 Answers
          4






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          Instead of writing output directly to the console, why not spawn a GUI window?



          Then, just have one area where output is directed, and a separate input area at the bottom where you can type commands. Kinda like how an irc client would look.



          If it has to be console only, I would suggest using something like ncurses (or PDCurses) to create a text based interface.






          share|improve this answer




























            up vote
            1
            down vote













            Without thinking too hard about this, it seems to me you either need a non-blocking input routine for stdin, something like getch() but just returns if there is nothing to read, which you call in a loop while also processing output, or you need two thraeds: one to read, one to write.






            share|improve this answer





















            • The input routine is non-blocking, hence the output appearing where you're inputting when you type. See my example, italics is what is outputted whilst I'm typing "Hello world"
              – Saul
              Jun 6 '09 at 20:05










            • You may use some kind of synchronization and block/buffer one while the other is happening, or just use two different areas for input and output. I highly suggest you take @AsLanFromNarnia's suggestion and opt for the latter.
              – aib
              Jun 7 '09 at 0:40


















            up vote
            0
            down vote













            You're developing the console yourself, so this should be no big problem.



            The console is basically a set of text lines that are being rendered. User input is echoed so the user sees wha he's typing. This means that the last line of the console is special, it is the "editable input buffer". All other lines are output. When the user hits Enter, you execute the edit buffer. Step 1 in executing is making a private copy of the edit buffer, step 2 is clearing the edit buffer, and step 3 is copying the private copy to output.



            Hence, at any moment there's only one partial line and it only changes by user input. All other lines are complete, and change on a line-by-line basis. Your program logging happens between two user inputs and therefore gets itws own line. In a multi-threaded program, this means the "Console::AddLine" function will need an internal mutex (CriticalSection for Win32).






            share|improve this answer




























              up vote
              0
              down vote













              It sounds like you need a layer between the code and your console. Create a wrapper object that does all the console I/O. When someone calls its WriteLine method, the wrapper should erase the currently displayed input (if any), write the line, and then write the input again beneath it.






              share|improve this answer





















                Your Answer






                StackExchange.ifUsing("editor", function () {
                StackExchange.using("externalEditor", function () {
                StackExchange.using("snippets", function () {
                StackExchange.snippets.init();
                });
                });
                }, "code-snippets");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "1"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                convertImagesToLinks: true,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: 10,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f959947%2fasynchronous-windows-console-input-whilst-outputting%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                4
                down vote



                accepted










                Instead of writing output directly to the console, why not spawn a GUI window?



                Then, just have one area where output is directed, and a separate input area at the bottom where you can type commands. Kinda like how an irc client would look.



                If it has to be console only, I would suggest using something like ncurses (or PDCurses) to create a text based interface.






                share|improve this answer

























                  up vote
                  4
                  down vote



                  accepted










                  Instead of writing output directly to the console, why not spawn a GUI window?



                  Then, just have one area where output is directed, and a separate input area at the bottom where you can type commands. Kinda like how an irc client would look.



                  If it has to be console only, I would suggest using something like ncurses (or PDCurses) to create a text based interface.






                  share|improve this answer























                    up vote
                    4
                    down vote



                    accepted







                    up vote
                    4
                    down vote



                    accepted






                    Instead of writing output directly to the console, why not spawn a GUI window?



                    Then, just have one area where output is directed, and a separate input area at the bottom where you can type commands. Kinda like how an irc client would look.



                    If it has to be console only, I would suggest using something like ncurses (or PDCurses) to create a text based interface.






                    share|improve this answer












                    Instead of writing output directly to the console, why not spawn a GUI window?



                    Then, just have one area where output is directed, and a separate input area at the bottom where you can type commands. Kinda like how an irc client would look.



                    If it has to be console only, I would suggest using something like ncurses (or PDCurses) to create a text based interface.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jun 6 '09 at 16:48









                    Fredrick Pennachi

                    727616




                    727616
























                        up vote
                        1
                        down vote













                        Without thinking too hard about this, it seems to me you either need a non-blocking input routine for stdin, something like getch() but just returns if there is nothing to read, which you call in a loop while also processing output, or you need two thraeds: one to read, one to write.






                        share|improve this answer





















                        • The input routine is non-blocking, hence the output appearing where you're inputting when you type. See my example, italics is what is outputted whilst I'm typing "Hello world"
                          – Saul
                          Jun 6 '09 at 20:05










                        • You may use some kind of synchronization and block/buffer one while the other is happening, or just use two different areas for input and output. I highly suggest you take @AsLanFromNarnia's suggestion and opt for the latter.
                          – aib
                          Jun 7 '09 at 0:40















                        up vote
                        1
                        down vote













                        Without thinking too hard about this, it seems to me you either need a non-blocking input routine for stdin, something like getch() but just returns if there is nothing to read, which you call in a loop while also processing output, or you need two thraeds: one to read, one to write.






                        share|improve this answer





















                        • The input routine is non-blocking, hence the output appearing where you're inputting when you type. See my example, italics is what is outputted whilst I'm typing "Hello world"
                          – Saul
                          Jun 6 '09 at 20:05










                        • You may use some kind of synchronization and block/buffer one while the other is happening, or just use two different areas for input and output. I highly suggest you take @AsLanFromNarnia's suggestion and opt for the latter.
                          – aib
                          Jun 7 '09 at 0:40













                        up vote
                        1
                        down vote










                        up vote
                        1
                        down vote









                        Without thinking too hard about this, it seems to me you either need a non-blocking input routine for stdin, something like getch() but just returns if there is nothing to read, which you call in a loop while also processing output, or you need two thraeds: one to read, one to write.






                        share|improve this answer












                        Without thinking too hard about this, it seems to me you either need a non-blocking input routine for stdin, something like getch() but just returns if there is nothing to read, which you call in a loop while also processing output, or you need two thraeds: one to read, one to write.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Jun 6 '09 at 16:55









                        i_am_jorf

                        44.4k11111204




                        44.4k11111204












                        • The input routine is non-blocking, hence the output appearing where you're inputting when you type. See my example, italics is what is outputted whilst I'm typing "Hello world"
                          – Saul
                          Jun 6 '09 at 20:05










                        • You may use some kind of synchronization and block/buffer one while the other is happening, or just use two different areas for input and output. I highly suggest you take @AsLanFromNarnia's suggestion and opt for the latter.
                          – aib
                          Jun 7 '09 at 0:40


















                        • The input routine is non-blocking, hence the output appearing where you're inputting when you type. See my example, italics is what is outputted whilst I'm typing "Hello world"
                          – Saul
                          Jun 6 '09 at 20:05










                        • You may use some kind of synchronization and block/buffer one while the other is happening, or just use two different areas for input and output. I highly suggest you take @AsLanFromNarnia's suggestion and opt for the latter.
                          – aib
                          Jun 7 '09 at 0:40
















                        The input routine is non-blocking, hence the output appearing where you're inputting when you type. See my example, italics is what is outputted whilst I'm typing "Hello world"
                        – Saul
                        Jun 6 '09 at 20:05




                        The input routine is non-blocking, hence the output appearing where you're inputting when you type. See my example, italics is what is outputted whilst I'm typing "Hello world"
                        – Saul
                        Jun 6 '09 at 20:05












                        You may use some kind of synchronization and block/buffer one while the other is happening, or just use two different areas for input and output. I highly suggest you take @AsLanFromNarnia's suggestion and opt for the latter.
                        – aib
                        Jun 7 '09 at 0:40




                        You may use some kind of synchronization and block/buffer one while the other is happening, or just use two different areas for input and output. I highly suggest you take @AsLanFromNarnia's suggestion and opt for the latter.
                        – aib
                        Jun 7 '09 at 0:40










                        up vote
                        0
                        down vote













                        You're developing the console yourself, so this should be no big problem.



                        The console is basically a set of text lines that are being rendered. User input is echoed so the user sees wha he's typing. This means that the last line of the console is special, it is the "editable input buffer". All other lines are output. When the user hits Enter, you execute the edit buffer. Step 1 in executing is making a private copy of the edit buffer, step 2 is clearing the edit buffer, and step 3 is copying the private copy to output.



                        Hence, at any moment there's only one partial line and it only changes by user input. All other lines are complete, and change on a line-by-line basis. Your program logging happens between two user inputs and therefore gets itws own line. In a multi-threaded program, this means the "Console::AddLine" function will need an internal mutex (CriticalSection for Win32).






                        share|improve this answer

























                          up vote
                          0
                          down vote













                          You're developing the console yourself, so this should be no big problem.



                          The console is basically a set of text lines that are being rendered. User input is echoed so the user sees wha he's typing. This means that the last line of the console is special, it is the "editable input buffer". All other lines are output. When the user hits Enter, you execute the edit buffer. Step 1 in executing is making a private copy of the edit buffer, step 2 is clearing the edit buffer, and step 3 is copying the private copy to output.



                          Hence, at any moment there's only one partial line and it only changes by user input. All other lines are complete, and change on a line-by-line basis. Your program logging happens between two user inputs and therefore gets itws own line. In a multi-threaded program, this means the "Console::AddLine" function will need an internal mutex (CriticalSection for Win32).






                          share|improve this answer























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            You're developing the console yourself, so this should be no big problem.



                            The console is basically a set of text lines that are being rendered. User input is echoed so the user sees wha he's typing. This means that the last line of the console is special, it is the "editable input buffer". All other lines are output. When the user hits Enter, you execute the edit buffer. Step 1 in executing is making a private copy of the edit buffer, step 2 is clearing the edit buffer, and step 3 is copying the private copy to output.



                            Hence, at any moment there's only one partial line and it only changes by user input. All other lines are complete, and change on a line-by-line basis. Your program logging happens between two user inputs and therefore gets itws own line. In a multi-threaded program, this means the "Console::AddLine" function will need an internal mutex (CriticalSection for Win32).






                            share|improve this answer












                            You're developing the console yourself, so this should be no big problem.



                            The console is basically a set of text lines that are being rendered. User input is echoed so the user sees wha he's typing. This means that the last line of the console is special, it is the "editable input buffer". All other lines are output. When the user hits Enter, you execute the edit buffer. Step 1 in executing is making a private copy of the edit buffer, step 2 is clearing the edit buffer, and step 3 is copying the private copy to output.



                            Hence, at any moment there's only one partial line and it only changes by user input. All other lines are complete, and change on a line-by-line basis. Your program logging happens between two user inputs and therefore gets itws own line. In a multi-threaded program, this means the "Console::AddLine" function will need an internal mutex (CriticalSection for Win32).







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jun 8 '09 at 8:53









                            MSalters

                            133k8115267




                            133k8115267






















                                up vote
                                0
                                down vote













                                It sounds like you need a layer between the code and your console. Create a wrapper object that does all the console I/O. When someone calls its WriteLine method, the wrapper should erase the currently displayed input (if any), write the line, and then write the input again beneath it.






                                share|improve this answer

























                                  up vote
                                  0
                                  down vote













                                  It sounds like you need a layer between the code and your console. Create a wrapper object that does all the console I/O. When someone calls its WriteLine method, the wrapper should erase the currently displayed input (if any), write the line, and then write the input again beneath it.






                                  share|improve this answer























                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    It sounds like you need a layer between the code and your console. Create a wrapper object that does all the console I/O. When someone calls its WriteLine method, the wrapper should erase the currently displayed input (if any), write the line, and then write the input again beneath it.






                                    share|improve this answer












                                    It sounds like you need a layer between the code and your console. Create a wrapper object that does all the console I/O. When someone calls its WriteLine method, the wrapper should erase the currently displayed input (if any), write the line, and then write the input again beneath it.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Jun 8 '09 at 20:31









                                    Peter Ruderman

                                    10.1k2352




                                    10.1k2352






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Stack Overflow!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.





                                        Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                        Please pay close attention to the following guidance:


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f959947%2fasynchronous-windows-console-input-whilst-outputting%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Bressuire

                                        Vorschmack

                                        Quarantine