I have class Client, which is receives data from Stream.
Also I have MainWindow, which has ListBox.
Basically I need to add item in ListBox when new data received. Please see codes (pay attention to TODO lines)
class Client
{
private TcpClient client;
private NetworkStream stream;
private ASCIIEncoding encoder;
private Thread clientThread;
public Client(string ip, int port)
{
this.client = new TcpClient(ip, port);
this.stream = client.GetStream();
this.encoder = new ASCIIEncoding();
clientThread = new Thread(Receive);
clientThread.Start();
}
public void Receive()
{
byte[] data = new byte[4096];
int bytesRead;
bytesRead = stream.Read(data, 0, 4096);
string[] message = DecodeMessage(data, bytesRead);
// TODO: Notify about new message
}
Here is my MainWindow
public partial class MainWindow : Window
{
Client client;
public MainWindow()
{
InitializeComponent();
client = new Client("127.0.0.1", 2020);
}
public void updateChat(string[] message)
{
// TODO: Should add new messages into ListBox
}
private void send_Click(object sender, RoutedEventArgs e)
{
client.Send(0, this.messageBox.Text);
this.messageBox.Clear();
}
}
Please see the code bellow paying attention to parts surrounded by stars.
Please read this as well: Events (C# Programming Guide)
http://msdn.microsoft.com/en-us/library/awbftdfh.aspx