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.

Win Coder
  • 6,628
  • 11
  • 54
  • 81
  • [Open a new console with every new Thread in C#?](http://stackoverflow.com/questions/8023796/open-a-new-console-with-every-new-thread-in-c) – L.B Oct 30 '12 at 19:49
  • 1
    You'll need to figure out how you want the program to respond, before we can help you implement that. Do you want information printed out to wait until they finish inputting information before it is displayed? That can be achieved through a few `lock` statements. Do you want a separate area for displaying text and inputting it? That will need a more involved UI than a shell, in all probability. Consider a form with two multi-line textboxes. If you want to mimic that functionality in the Console it will be...difficult. – Servy Oct 30 '12 at 19:53
  • @L.B ok...this is bad. So i guessing what i want is not possible ? – Win Coder Oct 30 '12 at 19:55
  • @WinCoder You haven't told us what you want, you've just told us what you've done and said it doesn't do what you want. – Servy Oct 30 '12 at 19:55
  • @Servy In a nutshell i just want a clone of facebook chat is it possible using console ? – Win Coder Oct 30 '12 at 19:56
  • 1
    facebook chat isn't implemented in a console. I've never personally used a shell-based IM client, I've only used IM clients written in a web/desktop UI. If you want to mimic that you're better off just making a winform/WPF application instead. Like I said, just put two textboxes on a form/window and you're pretty set. – Servy Oct 30 '12 at 19:57
  • @Servy Well ultimately i do have to make the client in winforms. But before that i just wanted to do it in the console. I guess its much more difficult than it appears or even impossible. – Win Coder Oct 30 '12 at 19:59
  • @WinCoder It's not *impossible* it's just much, much more difficult to implement in a Console than in a winform. – Servy Oct 30 '12 at 20:02
  • If you can just point me in the right direction.Even though i have to do it on forms, i still wanna check out the console version too. and many Thanks for your help, and also thanks to the makers of internet and this site. I don't know how many more hours would i have wasted on solving this problem by myself. – Win Coder Oct 30 '12 at 20:06
  • @WinCoder I just finished posting an answer summarizing my comments, as well as what it would take to do this in a console. – Servy Oct 30 '12 at 20:08

3 Answers3

2

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.

Servy
  • 202,030
  • 26
  • 332
  • 449
1

It's not practical to depict a chatting like application using Console. If you really want to see it working try using WinForm or WPF application, there you have plenty of options in the form of Controls to show facebook like chatting in action like using ListBox controls, etc.

Obviously this is one way but it is really difficult to achieve it in Console.

Servy
  • 202,030
  • 26
  • 332
  • 449
Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
0

Lol 2ez

    public static void WriteLineMultithread(string strt) {
        int lastx=Console.CursorLeft,lasty=Console.CursorTop;

        Console.MoveBufferArea(0,lasty,lastx,1,0,lasty+1,' ',Console.ForegroundColor,Console.BackgroundColor);
        Console.SetCursorPosition(0,lasty);
        Console.WriteLine(strt);
        Console.SetCursorPosition(lastx,lasty+1);
    }

This method allow you to write a message while the user is using a "Console.ReadLine()" on another thread. I assume that the reception of your chatting message is asynchronous, or happen on another thread.

You can add some lock(){} and that would be perfect.

Melcx
  • 9
  • 2