Click here to Skip to main content
15,895,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello every one ,
Guys I just built a simple udp server and that's the code :


private void Form1_Load(object sender, EventArgs e)
        {
            Thread thdUDPServer = new Thread(new
ThreadStart(serverThread));
            thdUDPServer.Start();
        }

        public void serverThread()
{
UdpClient udpClient = new UdpClient(8080);
while(true)
{
    IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
    Byte[] receiveBytes = udpClient.Receive(ref
RemoteIpEndPoint);
    string returnData = Encoding.ASCII.GetString(receiveBytes);
    lbConnections.Items.Add(
    RemoteIpEndPoint.Address.ToString() + ":" +
    returnData.ToString()
    );
}
}
    }
}


But every time when i run the program the following error appears:

Cross-thread operation not valid: Control 'lbConnections' accessed from a thread other than the thread it was created on.

so please help me
Posted
Updated 8-Jan-11 6:23am
v2

1 solution

Gui is in one thread - so you need to "Invoke" see:
Getting Around InvokeRequired Without Copy and Paste[^]
For more info ... or here for something simpler[^]

Regards
Espen Harlinn
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900