private void button21_Click(object sender, RoutedEventArgs e) { if (textbox21.Text != "") { try { p_Message sMessage = new p_Message(listbox1.SelectedValue, textbox21.Text); sockclient.Send(sMessage.MakePocket()); } catch (SocketException) { MessageBox.Show("Cервер отключился. Пожалуйста дождитесь его переподключения", "Cервер Отключился", MessageBoxButton.OK, MessageBoxImage.Error); panel1.Visibility = System.Windows.Visibility.Visible; panel2.Visibility = System.Windows.Visibility.Hidden; sockclient.Disconnect(true); DoWork = false; } TextRange tr1 = new TextRange(richtextbox21.Document.ContentEnd, richtextbox21.Document.ContentEnd); tr1.Text = Environment.NewLine + "You: "; tr1.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue); TextRange tr2 = new TextRange(richtextbox21.Document.ContentEnd, richtextbox21.Document.ContentEnd); tr2.Text = textbox21.Text; tr2.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black); File.AppendAllText(yname + "\\" + Convert.ToString(listbox1.SelectedValue) + ".txt", Environment.NewLine + "You: " + textbox21.Text); richtextbox21.ScrollToEnd(); textbox21.Text = ""; } }
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; while (DoWork == true) { try { nbyte = sockclient.Receive(bts); } catch (SocketException) { continue; } if (nbyte != 0) { switch (int.Parse(ASCIIEncoding.GetEncoding(1251).GetString(bts, 0, 1))) { case 2: { p_Message rMessage = new p_Message(bts); s = ASCIIEncoding.GetEncoding(1251).GetString(bts, 0, nbyte); sname = rMessage.sender; s = rMessage.message; File.AppendAllText(yname + "\\" + sname + ".txt", Environment.NewLine + sname + ": " + s); backgroundworker1.ReportProgress(1); nbyte = 0; break; } case 3: { p_UserList rUserList = new p_UserList(bts); names.Clear(); names.AddRange(rUserList.names); backgroundworker1.ReportProgress(3); nbyte = 0; break; } } } Thread.Sleep(10); } }