Jump to content


Photo

Threading, Conflitos ...


  • Faça o login para participar
3 replies to this topic

#1 LucasMS

LucasMS

    Super Veterano

  • Usuários
  • 2076 posts
  • Sexo:Não informado

Posted 16/01/2006, 13:10

ola

tenho um programa de chat, onde faço um thread pra ficar lendo mensagens de um StreamReader. só que tem um problema: a funcao que vai no ThreadStart "nao consegue" alterar a variavel text de um richtextbox. alguem pode me ajudar?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;
using System.Threading;

namespace ChatClient
{

    public partial class Form1 : Form
        
    {
        private StreamReader sr;
        private StreamWriter sw;
        public Form1()
        {
            try
            {
                TcpClient client = new TcpClient("localhost", 12345);
                NetworkStream ns = client.GetStream();
                sr = new StreamReader(ns);
                sw = new StreamWriter(ns);
            }
            catch (Exception e) { MessageBox.Show(e.ToString()); }
            
            InitializeComponent(); 
            Thread t = new Thread(new ThreadStart(readMess));
            t.Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string msg = textBox1.Text;
            try
            {
                textBox1.Text = "";
                sw.WriteLine(msg);
                sw.Flush();
                textBox1.Focus();
            }
            catch (Exception ee) { MessageBox.Show(ee.ToString()); }
        }
        public void chatText(string text)
        {
        // AQUI APRESENTA O PROBLEMA: InvalidOperationException
            chat.Text+=text;
        }
        public void readMess()
        {
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                chatText(line);
            }
            Thread.Sleep(100);
        }
    }
}


#2 Washington Morais

Washington Morais

    Novato no fórum

  • Usuários
  • 16 posts
  • Sexo:Não informado
  • Localidade:Belo Horizonte

Posted 16/01/2006, 17:19

Olá...

Antes de alterar o valor do controle, tente liberar o mesmo:
public void chatText(string text)
{
   chat.SelectionProtected = false;
   chat.Text+=text;
}


:)
_________________________________________
Washington Morais
Desenvolvedor .NET
BHS (www.bhs.com.br)

Visitem DotNetRaptors

#3 felipecm

felipecm

    Expert

  • Usuários
  • 541 posts
  • Sexo:Não informado
  • Localidade:ABC / SP

Posted 16/01/2006, 20:32

ola

tenho um programa de chat, onde faço um thread pra ficar lendo mensagens de um StreamReader. só que tem um problema: a funcao que vai no ThreadStart "nao consegue" alterar a variavel text de um richtextbox. alguem pode me ajudar?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Sockets;
using System.Threading;

namespace ChatClient
{

    public partial class Form1 : Form
        
    {
        private StreamReader sr;
        private StreamWriter sw;
        public Form1()
        {
            try
            {
                TcpClient client = new TcpClient("localhost", 12345);
                NetworkStream ns = client.GetStream();
                sr = new StreamReader(ns);
                sw = new StreamWriter(ns);
            }
            catch (Exception e) { MessageBox.Show(e.ToString()); }
            
            InitializeComponent(); 
            Thread t = new Thread(new ThreadStart(readMess));
            t.Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string msg = textBox1.Text;
            try
            {
                textBox1.Text = "";
                sw.WriteLine(msg);
                sw.Flush();
                textBox1.Focus();
            }
            catch (Exception ee) { MessageBox.Show(ee.ToString()); }
        }
        public void chatText(string text)
        {
        // AQUI APRESENTA O PROBLEMA: InvalidOperationException
            chat.Text+=text;
        }
        public void readMess()
        {
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                chatText(line);
            }
            Thread.Sleep(100);
        }
    }
}

Está certo, me deparei com um problema semelhante ao migrar uma aplicação 2003 para o 2005. O problema ocorre pq a Thread nao consegue "enxergar" algo que foi criado em outra thread (fora dela no caso). Uma solução é trabalhar com delegates.
MCAD, MCP

#4 LucasMS

LucasMS

    Super Veterano

  • Usuários
  • 2076 posts
  • Sexo:Não informado

Posted 17/01/2006, 10:21

achei no MSDN a variavel
CheckForIllegalCrossThreadCalls = false;

agora ta funcionando direito. valeu ae ;)




0 user(s) are reading this topic

0 membro(s), 0 visitante(s) e 0 membros anônimo(s)

IPB Skin By Virteq