private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e) // Send Data { if (client.Connected) { STW.WriteLine(text_to_send); this.RecievedMessagetextBox.Invoke(new MethodInvoker(delegate() { RecievedMessagetextBox.AppendText("Ambulance Team : " + text_to_send + "\n"); // States user who is sending message and then then appends that message to it })); } else { MessageBox.Show("Send Failed Good Sir!"); // Send message error } backgroundWorker2.CancelAsync(); }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) // Recieve Data { while (client.Connected) { try { recieve = STR.ReadLine(); this.RecievedMessagetextBox.Invoke(new MethodInvoker(delegate() { RecievedMessagetextBox.AppendText("Regional Hospital : " + recieve + "\n"); // States user who is sending message and then then appends that message to it })); recieve = ""; // recieve is whatever the typer types in. } catch (Exception x) { MessageBox.Show(x.Message.ToString()); // Send error message if something is wrong. } } }
private void Connectbutton_Click(object sender, EventArgs e) // Connect to Server via Client { client = new TcpClient(); IPEndPoint IP_End = new IPEndPoint(IPAddress.Parse(ClientIPtextBox.Text), int.Parse(ClientPorttextBox.Text)); //Turns Port Int number into String try { client.Connect(IP_End); if (client.Connected) { RecievedMessagetextBox.AppendText("CONNECTED TO KWIKMEDICAL SERVER" + "\n"); // Displays This message when Client connects to Server STW = new StreamWriter(client.GetStream()); STR = new StreamReader(client.GetStream()); STW.AutoFlush = true; backgroundWorker1.RunWorkerAsync(); // Start recieveing Data in Background without interupting the UI backgroundWorker2.WorkerSupportsCancellation = true; // Ability to cancel this thread. } } catch (Exception x) { MessageBox.Show(x.Message.ToString()); } }