8
Reply

help with Threading with textboxes

Wallance Toh

Wallance Toh

Oct 7 2008 4:53 AM
7.7k
hi, im new here.
i have a thread, with textboxes, i have invoker which invokes when it is required.
when it invokes, the text was able to show, but if it doesn't require invoke, i just wrote

textbox1.Text += "thestring";

but the textbox was unable to show, anyone know how to solve it?

my textbox code it's under a function, where my thread calls it.


 public void updateit(string message)
 {
if (txtrecieve.IsDisposed) return;
if (txtrecieve.InvokeRequired)
            {
               
                    invoker = new GeneralDelegate(MethodRunningInMainThread);
                    txtrecieve.Invoke(invoker, new object[] { message });

                  
            }
            else
            {
                   txtrecieve.Text +=  message;
            }
}

my thread

 private void Listen()
        {
            Chat_Listen cl = new Chat_Listen(l2);

            while (true)
            {
               
                myport = cl.returnport();
  
                string message = cl.Listen(pass, salt, init);
                string[] part = message.Split(';');

                if (part[0] == "sendmsg2")
                {
                    string username = part[2].ToString() + ":" + part[3].ToString();
                    string msgRecieve = " Says: " + part[1].ToString();
                    string message1 = username + msgRecieve;
                    updateit(message1);
                 }

              }
            }

the one that activates the thread

 private void Chat_Load(object sender, EventArgs e)
        {

            if (!this.IsHandleCreated)
            {
                lock (this)
                {
                    this.CreateHandle();
                    this.CreateControl();
                    this.CreateGraphics();
                }
            }

            dp_local.Image = local;
            //dp_remote.Image = remoteimg;
            this.Text = "Talking to: " + remoteNickname;

             Sender = new Thread(new ThreadStart(Send));
           
            Listener = new Thread(new ThreadStart(Listen));
            //Listener.IsBackground = true;
            Listener.Start();
           
        }

thanks.


Answers (8)