public static void chatFensterThread(Object fensterObj) { ChatForm chatform = (ChatForm)fensterObj; foreach (StateObject item in Form1.chatObjekte) { if (chatform == item.chatForm) { chatform.Text = "Chat mit " + item.peerName; chatform.ShowDialog(); } } //Application.Run(chatform); }
private void ReceiveCallback(IAsyncResult ar) { try { sendDone.Set(); // Retrieve the state object and the client socket // from the asynchronous state object. StateObject state = (StateObject)ar.AsyncState; Socket client = state.workSocket; state.sb = new StringBuilder(); // Read data from the remote device. int bytesRead = client.EndReceive(ar); if (bytesRead > 0) { // There might be more data, so store the data received so far. state.sb.Append(Encoding.UTF8.GetString(state.buffer, 0, bytesRead)); string content = state.sb.ToString(); if (content.IndexOf("}end") > -1) { // All the data has been read from the // client. Display it on the console. Console.WriteLine("Read {0} bytes from socket. \n Data : {1}", content.Length, content); // Echo the data back to the client. int begin = content.IndexOf("beg{"); int ende = content.IndexOf("}end"); string contentOhneHeaderUndTailer = ""; for (int j = begin + 4; j < ende; j++) { contentOhneHeaderUndTailer += content[j]; } int aktion = -1; aktion = Int32.Parse("" + contentOhneHeaderUndTailer[0]); if (aktion == (int)aktionEnum.chatMessage) { string[] aufgeteilteNachricht = content.Split('☻'); string empfangenerChatText = aufgeteilteNachricht[3]; foreach (StateObject item in Form1.chatObjekte) { if (item.peerName.Contains(aufgeteilteNachricht[1])) { try { item.chatForm.Invoke((MethodInvoker) delegate { item.chatForm.Activate(); item.chatForm.chatText.Text = item.chatForm.chatText.Text + Environment.NewLine + Environment.NewLine + aufgeteilteNachricht[1] + ": " + empfangenerChatText; item.chatForm.chatText.SelectionStart = item.chatForm.chatText.Text.Length; item.chatForm.chatText.ScrollToCaret(); }); } catch (Exception ex) { Console.WriteLine(ex.ToString()); try { ChatForm chatformNeu = new ChatForm(); chatformNeu.Text = "Chat mit " + aufgeteilteNachricht[1]; item.chatForm = chatformNeu; chatformNeu.chatText.Text = item.chatForm.chatText.Text + Environment.NewLine + Environment.NewLine + aufgeteilteNachricht[1] + ": " + empfangenerChatText; chatformNeu.ShowDialog(); item.chatForm.Activate(); //Application.Run(chatform); } catch (Exception ex2) { Console.WriteLine(ex2.ToString()); } } break; } } } } } client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }