problem retrieving data processed by class library
Pls help! how am i going to update a form's listbox with data handled and stored by a class library ( the messages are from a connected socket)?
I want this listbox to diplay chat messages.
class X (class library code)
properties: ipadd,port,isconnected...etc
methods:
Connect() and ConnectCallback //will establish connection to the socket using BeginConnect and EndConnect
WaitForData and OnDataReceived(Callback) //will received the messages using BeginReceive and EndReceive
public void WaitForData()
{
recieveCallback = new AsyncCallback(onDataReceived);
StateObject socket = new StateObject();
socket.workSocket = mainClientSocket;
result = mainClientSocket.BeginReceive(socket.buffer, 0, socket.buffer.Length, SocketFlags.None, recieveCallback, socket);
}
private void onDataReceived(IAsyncResult result)
{
StateObject socket = (StateObject)result.AsyncState;
if (mainClientSocket.Connected)
{
bytesRcvd = socket.workSocket.EndReceive(result);
ReadXML(bytesReceived, socket); //method that will parse the message
receiveDone.Set();
WaitForData();
}
else
{
socket.workSocket.Close();
}
}
private void ReadXML(int bytes, StateObject socket)
{
...
...
...
Once data is parsed , it will be stored in SenderMessageList ///(List<string>)
SenderMessageQueue(Sender + ": " + Message);
}
private void SenderMessageQueue(String message, String socketID)
{
SenderMessageList.Add(message);
}
public String getSenderMessage //// I will call this property in a form to display //// the stored message
{
get
{
return SenderMessageList[SenderMessageList.Count - 1];
}
}
Form // will use the library
namespace WindowsFormsApplication4
{
using X;
..
..
x= new X.ClientSocket();
IntPtr handle = this.Handle;
x.IPadd = "127.0.0.1";
x.Port = 8222;
x.Connect();
ThreadStart threadStarter = new ThreadStart(ReceiveMessages);
myThread = new Thread(threadStarter);
myThread.IsBackground = true;
myThread.Start();
}
private void ReceiveMessages()
{
while (x.bytesReceived != 0)
{
listBox1.Invoke(new UpdateTextCallback(this.Update_listBox1), new object[] { a });
}
listBox1.Invoke(new UpdateTextCallback(this.Update_listBox1), new object[] { x.getSenderMessage });
}
private void Update_listBox1(String text)
{
listBox1.Items.Add(text);
}
private void ButtonSend_click{
}
When i move the listbox.invoke in ButtonSend, i can get the stored message..But what i want to do is automate the receiving of data..Sending has no problem cause it's working...I am by the way creating a chat program..i want to see all the messages coming in...Thanks.