Hi,
I am testing a Tcp client C# windows form application I am able to send data to Tcp server Software(Hercules 3.2.8) But when i sent data from Tcp server to windows form then i am getting the following error:

System.InvalidOperationException: Cross-Thread operation not valid: Control 'MessageList' accessed from a thread other than the threa it was control on. at System.Windows.Forms.ListBox.ObjectCollection.Add(Object item)
at WindowsFormsAppication1.Form1.MessageCallBack(IAsyncResult ar) in From1.cs:line 55

Line 44 is at Messagelist.items.Add(response);

public partial class Form1 : Form
    {
        Socket sck;
        EndPoint remoteEp;
        byte[] buffer;
        IPAddress ip = IPAddress.Parse("192.168.42.143");
        int port = 8061;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            RemoteIptxt.Text = ip.ToString();
            Remoteporttxt.Text = port.ToString();
            //setup socket
            sck = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
        }

        private void Connectbtn_Click(object sender, EventArgs e)
        {
            //binding socket
            remoteEp = new IPEndPoint(IPAddress.Parse(RemoteIptxt.Text), Convert.ToInt32(Remoteporttxt.Text));
            sck.Connect(remoteEp);

            buffer = new byte[1500];
            sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref remoteEp, new AsyncCallback(MessageCallBack), buffer);
        }

        private void MessageCallBack(IAsyncResult ar)
        {
            try
            {
                byte[] receiveData = new byte[1500];
                receiveData = (byte[])ar.AsyncState;
                //Converting byte[] to string
                ASCIIEncoding ascencoding = new ASCIIEncoding();
                string response = ascencoding.GetString(receiveData);

                //Adding message to listbox
                MessageList.Items.Add(response);

                buffer = new byte[1500];
                sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref remoteEp, new AsyncCallback(MessageCallBack), buffer);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        private void buttonSend_Click(object sender, EventArgs e)
        {
            //convert string message to byte[]
            ASCIIEncoding ascencoding = new ASCIIEncoding();
            byte[] sendmess = new byte[1500];
            sendmess = ascencoding.GetBytes(textMessage.Text);
            sck.Send(sendmess);
            MessageList.Items.Add("You Said:" + textMessage.Text);
            textMessage.Text = "";
        }

    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.