I am looking for a way to update a GUI from a class that I want to be stand alone and not rely on the GUI. It is a network class consisting of both a listener and a client. This class can connect and then send/receive data. I would like this data to be able to be displayed using a GUI but not have any of the GUI related code within the class itself.
So in short the network class only knows of itself. The GUI knows of the network class.
This is where I would like to add the code
public void ReceiveBytes()
{
byte[] receivedPacket;
while (IsListeningForBytes)
{
receivedPacket = new byte[Packet.BUFFERSIZE];
try
{
int bytesRead = ClientSocket.Receive(receivedPacket, SocketFlags.None);
if (bytesRead > 0)
{
SocketObject.ProcessMessage(receivedPacket);
// Update GUI here after the message has been processed.
}
else
{
throw new Exception("No bytes read");
}
}
catch (Exception ex)
{
IsListeningForBytes = false;
Disconnect();
Console.WriteLine(ex.Message);
}
}
}
Edit: Sorry everyone I will try and make things clearer. I am using windows forms and for the sake of this exercise we'll say I have three different controls: a listbox, a combobox and a textbox, that will take the data dependant of what is sent. (My actual application has listboxes, combo boxes, checkboxes ect that will need to be updated).
I understand that I shouldn't reference the GUI from within my object, hence my question.
As for relevant code, I'm not sure what you would like to see.
I have read about eventhandlers and delegates but I'm unsure how to actually implement it in this case. I originally passed a GUI update method as an action to the class that was called when required but this seemed long winded and didn't specifically update the control that I wanted.
Thanks for your help so far.