none
console.ReadLine() RRS feed

  • Question

  • Hi, i got multiple threads for a chat server i been working on for fun.

    When i do Console.ReadLine() and a another thread does console.WriteLine() it "breaks" the console.readline() part. Anything i can replace it with?

    I was thinking of making a Form for the server but i dont know if that is necessary..

    Sunday, January 21, 2018 12:41 PM

All replies

  • By the way, does anyone know if you can use the ~ for a function when a void is done?

    like void command(){

    I do stuff here

    }

    ~ command(){

    Console.WriteLine("command void done!");

    }

    Sunday, January 21, 2018 12:45 PM
  • Probably it is a mistake to share the console among threads. You could create a queue of data to go to the console and your main thread could control I/O so that during user input the console is not disturbed. Or you can use console functions to split the console into separate parts; I am not aware of .Net supporting the console functions that the Windows API has. Unix and Linux have a variety of ways to make a console (in olden days they were called terminals) be interactive; see curses for an example. Go to Console documentation and read all the documentation in that section and then if you have questions ask in the Windows API forum (whatever it is called now). Or you can switch to a GUI interface; you are more likely to get help with that.


    Sam Hobbs
    SimpleSamples.Info

    • Proposed as answer by Cor Ligthert Thursday, January 25, 2018 10:41 AM
    Sunday, January 21, 2018 7:14 PM
  • By the way, does anyone know if you can use the ~ for a function when a void is done?

    like void command(){

    I do stuff here

    }

    ~ command(){

    Console.WriteLine("command void done!");

    }


    This is the C# forum, not the C++ forum.


    Sam Hobbs
    SimpleSamples.Info

    Sunday, January 21, 2018 7:14 PM
  • Hello KoffeSwed,

    Please give me your code info. In my test the Console.ReadLine method didn't read the output value of Console.WriteLine. There is my tested code.

     class Program
        {
            static void Main(string[] args)
            {
                Thread thread = new Thread(Test) { IsBackground=true};
                thread.Start();
           
                string str = Console.ReadLine();               
                Console.WriteLine("step1");
                string str1 = Console.ReadLine();
            }    
            private static void Test()
            {
                while (true) {
                    Thread.Sleep(1000);
                    
                    Console.WriteLine("ttttttt");
                }          
            }
        }


    As for your second question, the "~" means the method is destructor, which will be invoked when the instance is recycled or deleted. For more details you could see here.

        class Test {
         public  Test() {
                Console.WriteLine("tttttt");
            }
            ~Test() {
                Console.WriteLine("sssss");
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Test test = new Test();
                test = null;        
                GC.Collect();
                Console.ReadLine();
             }
         }

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, January 22, 2018 8:13 AM
    Moderator
  • Even in C++, the ~ destructor only applies to classes, not to functions.  You can simulate that by using try/finally:

        void command() {
            try {
                // I do stuff here
            }
            finally {
                Console.WriteLine( "command void done!" );
            }
        }

    When you say it "breaks" the console.readline, what do you mean?  It's going to print out the output in the middle of your typing, but your input should still be accepted.


    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Tuesday, January 23, 2018 9:41 PM
  • Hello KoffeSwed,

    Is there any update or any other assistance I could provide? You could mark the helpful reply as answer if the issue has been solved. And if you have any concerns, please do not hesitate to let us know.

    Thank a lot!

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 25, 2018 9:52 AM
    Moderator