private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e) { if (clientChat.Connected) { STW.WriteLine(TextToSends); this.TbChatBox.Invoke(new MethodInvoker(delegate() { TbChatBox.AppendText("Me:" + TextToSends + "\n"); })); } else { MessageBox.Show("Sending failed"); } backgroundWorker2.CancelAsync(); }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { //WerewolfModel wm = new WerewolfModel(); while (clientChat.Connected) { try { recieve = STR.ReadLine(); this.TbChatBox.Invoke(new MethodInvoker(delegate() { TbChatBox.AppendText("You:" + recieve + "\n"); })); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } }
private void bntConnectChat_Click(object sender, EventArgs e) { clientChat = new TcpClient(); IPEndPoint IpEnd = new IPEndPoint(IPAddress.Parse(ServerIPtextBox.Text), 1); try { clientChat.Connect(IpEnd); if (clientChat.Connected) { TbChatBox.AppendText("Connected to server" + "\n"); STR = new StreamReader(clientChat.GetStream()); STW = new StreamWriter(clientChat.GetStream()); STW.AutoFlush = true; backgroundWorker1.RunWorkerAsync(); backgroundWorker2.WorkerSupportsCancellation = true; } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } }
public void AddChatMessage(string str) { TbChatBox.AppendText(str + Environment.NewLine); }