need help in Basic Network programming in c#
-
Hello Experts, I would like to ask if what's wrong with my program. I have created 1 client and 1 server that will accept the message of the client but I got this error.
Cross-thread operation not valid: Control 'lbConnections' accessed from a thread other than the thread it was created on.
For more details here is my code for the Client that send message to server.
using System;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;namespace UDP_Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void button1\_Click(object sender, EventArgs e) { UdpClient udpClient = new UdpClient(); udpClient.Connect(tbHost.Text, 8080); Byte\[\] sendBytes = Encoding.ASCII.GetBytes("Hello World?"); udpClient.Send(sendBytes, sendBytes.Length); } private void Form1\_Load(object sender, EventArgs e) { } }
}
Here also is the code to my server which were I got the error thats is mention above.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net.Sockets;namespace UDP_Server
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}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() ); } } private void Form1\_Load(object sender, EventArgs e) { Thread thdUDPServer = new Thread(new ThreadStart(serverThread)); thdUDPServer.Start(); } }
}
Any comments or suggestion to help is kindly appreciated. Thanks, DAN
-
Hello Experts, I would like to ask if what's wrong with my program. I have created 1 client and 1 server that will accept the message of the client but I got this error.
Cross-thread operation not valid: Control 'lbConnections' accessed from a thread other than the thread it was created on.
For more details here is my code for the Client that send message to server.
using System;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;namespace UDP_Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}private void button1\_Click(object sender, EventArgs e) { UdpClient udpClient = new UdpClient(); udpClient.Connect(tbHost.Text, 8080); Byte\[\] sendBytes = Encoding.ASCII.GetBytes("Hello World?"); udpClient.Send(sendBytes, sendBytes.Length); } private void Form1\_Load(object sender, EventArgs e) { } }
}
Here also is the code to my server which were I got the error thats is mention above.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net.Sockets;namespace UDP_Server
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}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() ); } } private void Form1\_Load(object sender, EventArgs e) { Thread thdUDPServer = new Thread(new ThreadStart(serverThread)); thdUDPServer.Start(); } }
}
Any comments or suggestion to help is kindly appreciated. Thanks, DAN
You are trying to access UI controls(here listbox lbConnections) from a user thread, which is not possible in Winforms. Winforms allows the updation of controls from the thread that have created it. Refer below for a detailed explanation to it. http://weblogs.asp.net/justin_rogers/articles/126345.aspx[^] You can use Dispatcher in .Net 4.0 for updating the controls from a user thread. Here is the link for the same http://msdn.microsoft.com/en-us/library/ms741870.aspx[^] Hope this helps!
Praveen Raghuvanshi Software Developer