private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e) { if (cliente.Connected) { stw.WriteLine(info); this.Chatscreen.Invoke(new MethodInvoker(delegate() { Chatscreen.AppendText("Yo:" + info + "\n"); })); } else { MessageBox.Show("Fallo al enviar"); } backgroundWorker2.CancelAsync(); }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { while (cliente.Connected) { ; } { try { datos = str.ReadLine(); this.Chatscreen.Invoke(new MethodInvoker(delegate() { Chatscreen.AppendText("Tu:" + datos + "\n"); })); datos = ""; } catch (Exception error) { MessageBox.Show(error.Message.ToString()); } } }
private void btnconectar_Click(object sender, EventArgs e) { cliente = new TcpClient(); IPEndPoint ipend = new IPEndPoint(IPAddress.Parse(Ipcliente.Text), int.Parse(Puertocliente.Text)); try { cliente.Connect(ipend); if (cliente.Connected) { Chatscreen.AppendText("Conectado con el servidor" + "\n"); str = new StreamReader(cliente.GetStream()); stw = new StreamWriter(cliente.GetStream()); stw.AutoFlush = true; backgroundWorker1.RunWorkerAsync(); backgroundWorker2.WorkerSupportsCancellation = true; } } catch (Exception error) { MessageBox.Show(error.Message.ToString()); } }