Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8757165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:14:21+00:00 2026-06-13T14:14:21+00:00

I am trying to mock up of a chat client. First here’s the code

  • 0

I am trying to mock up of a chat client. First here’s the code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace thread
{
    class Program
    {

    public static Thread t1;
    public static Thread t2;
    public static bool flag;
    public static Random rand = new Random();

    static void Main(string[] args)
    {
        t1 = new Thread(first);
        t2 = new Thread(second);
        t1.Start();
        t2.Start();

        Console.Read();
    }

    public static void first()
    {

        string[] phrase = { "Hello", "random", "blah,blah", "computer", "Welcome", "This is chat bot" };
        while (!flag)
        {
            Thread.Sleep(4000);
            Console.WriteLine("{0}", phrase[rand.Next(6)]);
        }
    }

    public static void second()
    {
        string input = "";
        while (input!="x")
        {

            input=Console.ReadLine();
            if (input=="x")
            {
                break;
            }

        }
        flag = true;
    }


   }
}

Ok so this program would automatically print some text on console, and i can write my message on the screen too. Now the problem is that whenever i am typing a long sentence,
anything that takes more than 4 seconds to type. Then instead of the automated message being printed on the next line it just gets append to whatever i am typing. I am really new to multi threading so i am not exactly sure what’s the problem. I think both the threads are using the same console class.

Help would be appreciated in this regard.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T14:14:22+00:00Added an answer on June 13, 2026 at 2:14 pm

    Implementing a Chat client in the Console is very difficult. It’s possible, but it’s not at all trivial.

    It is much easier to implement it in a GUI-based environment, such as winforms, where you can have two entirely separate text areas, one for input and one for output.

    In order to do this in a console you would need to, whenever you needed to display test, move the cursor up to a previous line, write out that text, and then move the cursor back to where the user had it for input. But doing that would over-write the previous line of text, so that previous line of text would need to be written on the line before that, and so on and so forth until you get to the top of the buffer, where the line can be removed entirely. On top of that, you can’t read information from the console, so you’ll need to keep track of everything in memory so you can do this whole write out.

    Doing everything in a winform is much, much easier. To write out information just add it to the text in the output textbox, and to read information, when the “send” button or enter is pressed, just clear the input textbox and process it’s contents. You don’t need to worry about the interaction between those two.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to mock the following method using Jack. The code example is
I'm trying to mock out the System.net.Sockets.Socket class in C# - I tried using
I am trying to mock java.awt.Toolkit.beep() using JMockit Expectations. I have the following code
I have been trying to mock out a using with a file stream but
I've been trying to get to mock a method with vararg parameters using Mockito:
I have built a mock object using EasyMock, and I'm trying to have the
I'm trying to mock the SecurityManager class. When I run the following code, Mockito
I am trying to mock the server response for a POST request using Sinon.js.
I'm trying to mock up DomainContext calls for my unit test. My real code
I am trying to mock a class like so (thank's google for code design

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.