Monday, March 28, 2011

C# console program - stop STDIN from going to STDOUT

I'm writing a simple console app in C#, .NET 2.0. It starts new threads using a threading timer, while it interprets commands on the main thread.

I currently take three commands: P - Pause C - Continue Q - Quit

This functionality works quite well, but unfortunately when I type P, C, or Q (or any other character for that matter), the character goes to STDOUT. Is there a way I can stop this programatically? Also it'd be useful to know if I could disable and re-enable STDIN -> STDOUT.

Thanks in advance.

From stackoverflow
  • Sounds like you are using

    Console.ReadKey();
    

    Which clearly states in the documentation that it prints to the screen and if you don't want to output to the screen you should use the overloaded version

    Console.ReadKey(true);
    

    Which does not output.

    C. Ross : Thanks, sorry I didn't RTFM.
  • First idea, can you set up a separate stream so outputing to STDOUT will not interfere with your program? If not, you should be able to point STDOUT to a different file descriptor while your code is being executed:

      FILE *realStdout= STDOUT;
      STDOUT = fopen("filepath", "w");
      //code
      fclose(STDOUT);
      STDOUT = realStdout;
    
    C. Ross : Is this actually C# code? I don't think so ...

0 comments:

Post a Comment