Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. need help in Basic Network programming in c#

need help in Basic Network programming in c#

Scheduled Pinned Locked Moved C#
csharpsysadminhelplinqgraphics
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    akosidandan
    wrote on last edited by
    #1

    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

    P 1 Reply Last reply
    0
    • A akosidandan

      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

      P Offline
      P Offline
      Praveen Raghuvanshi
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups