problem retrieving data processed by class library

csharp , html , vb , sql Manila, Philippines
  • 10 years ago
    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) 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.

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Most software today is very much like an Egyptian pyramid with millions of bricks piled on top of each other, with no structural integrity, but just done by brute force and thousands of slaves” - Alan Kay