/// <summary> /// Provides a thread safe way to add a row to the data grid. /// </summary> private void SubmitPersonToDataGrid(PersonPackage person) { Invoke((Action) delegate { dataGridView.Rows.Add(person.Name, person.Age, person.IsMale); }); }
private void ReceiveCallback(IAsyncResult AR) { try { // Socket exception will raise here when client closes, as this sample does not // demonstrate graceful disconnects for the sake of simplicity. int received = clientSocket.EndReceive(AR); if (received == 0) { return; } // The received data is deserialized in the PersonPackage ctor. PersonPackage person = new PersonPackage(buffer); SubmitPersonToDataGrid(person); // Start receiving data again. clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCallback, null); } // Avoid Pokemon exception handling in cases like these. catch (SocketException ex) { ShowErrorDialog(ex.Message); } catch (ObjectDisposedException ex) { ShowErrorDialog(ex.Message); } }
private void ReceiveCallback(IAsyncResult AR) { try { // Socket exception will raise here when client closes, as this sample does not // demonstrate graceful disconnects for the sake of simplicity. int received = clientSocket.EndReceive(AR); if (received == 0) { return; } // The received data is deserialized in the PersonPackage ctor. PersonPackage person = new PersonPackage(buffer); PersonPackage server = new PersonPackage("Servidor", person.Text); byte[] bufferRespuesta = server.ToByteArray(); // Send a message to the newly connected client. clientSocket.BeginSend(bufferRespuesta, 0, bufferRespuesta.Length, SocketFlags.None, SendCallback, null); // Start receiving data again. clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCallback, null); } // Avoid Pokemon exception handling in cases like these. catch (SocketException ex) { ShowErrorDialog(ex.Message); } catch (ObjectDisposedException ex) { ShowErrorDialog(ex.Message); } }