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. Thread problem...

Thread problem...

Scheduled Pinned Locked Moved C#
csharpsysadminhelpquestionannouncement
5 Posts 4 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.
  • M Offline
    M Offline
    Mc_Topaz
    wrote on last edited by
    #1

    I'm developing a chat server and a chat client in C#. My client application is a GUI with some TabControls, ListBoxes, Buttons, Textboxes and Labels. The client also has a separate thread to detect messages sent from the server. One of the messages sent from the server is to display what other users are available to chat with. I can receive the other users IP-address and store them in a collection. When this is done I send an event from the thread method to the Client's GUI's Form-class and run a method to display the users. That method is just clearing all items in a ListBox and insert all IP-addresses into the ListBox by foreach-looping the collection sent as a parameter from the event. When I try this an: InvalidOperationException is thrown. The exception messages basically says this (translated from Swedish): "The actions between threads are not valid. The control lstOtherUsers (the name of the ListBox) are received from an other thread that it was created in" I can understand the problem why I cannot update the Item collection in my ListBox from an other thread when the ListBox was created in an other thread. But how shall I solve this?

    K L L 3 Replies Last reply
    0
    • M Mc_Topaz

      I'm developing a chat server and a chat client in C#. My client application is a GUI with some TabControls, ListBoxes, Buttons, Textboxes and Labels. The client also has a separate thread to detect messages sent from the server. One of the messages sent from the server is to display what other users are available to chat with. I can receive the other users IP-address and store them in a collection. When this is done I send an event from the thread method to the Client's GUI's Form-class and run a method to display the users. That method is just clearing all items in a ListBox and insert all IP-addresses into the ListBox by foreach-looping the collection sent as a parameter from the event. When I try this an: InvalidOperationException is thrown. The exception messages basically says this (translated from Swedish): "The actions between threads are not valid. The control lstOtherUsers (the name of the ListBox) are received from an other thread that it was created in" I can understand the problem why I cannot update the Item collection in my ListBox from an other thread when the ListBox was created in an other thread. But how shall I solve this?

      K Offline
      K Offline
      kstls
      wrote on last edited by
      #2

      What your experiencing is called Invalid cross-thread operations. You can read more about it here

      M 1 Reply Last reply
      0
      • M Mc_Topaz

        I'm developing a chat server and a chat client in C#. My client application is a GUI with some TabControls, ListBoxes, Buttons, Textboxes and Labels. The client also has a separate thread to detect messages sent from the server. One of the messages sent from the server is to display what other users are available to chat with. I can receive the other users IP-address and store them in a collection. When this is done I send an event from the thread method to the Client's GUI's Form-class and run a method to display the users. That method is just clearing all items in a ListBox and insert all IP-addresses into the ListBox by foreach-looping the collection sent as a parameter from the event. When I try this an: InvalidOperationException is thrown. The exception messages basically says this (translated from Swedish): "The actions between threads are not valid. The control lstOtherUsers (the name of the ListBox) are received from an other thread that it was created in" I can understand the problem why I cannot update the Item collection in my ListBox from an other thread when the ListBox was created in an other thread. But how shall I solve this?

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Hi, Here[^] are my recommendations. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


        1 Reply Last reply
        0
        • K kstls

          What your experiencing is called Invalid cross-thread operations. You can read more about it here

          M Offline
          M Offline
          Mc_Topaz
          wrote on last edited by
          #4

          Thanks!

          1 Reply Last reply
          0
          • M Mc_Topaz

            I'm developing a chat server and a chat client in C#. My client application is a GUI with some TabControls, ListBoxes, Buttons, Textboxes and Labels. The client also has a separate thread to detect messages sent from the server. One of the messages sent from the server is to display what other users are available to chat with. I can receive the other users IP-address and store them in a collection. When this is done I send an event from the thread method to the Client's GUI's Form-class and run a method to display the users. That method is just clearing all items in a ListBox and insert all IP-addresses into the ListBox by foreach-looping the collection sent as a parameter from the event. When I try this an: InvalidOperationException is thrown. The exception messages basically says this (translated from Swedish): "The actions between threads are not valid. The control lstOtherUsers (the name of the ListBox) are received from an other thread that it was created in" I can understand the problem why I cannot update the Item collection in my ListBox from an other thread when the ListBox was created in an other thread. But how shall I solve this?

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Controls can only be accessed from the thread they were created in. To access controls from other threads, use InvokeRequired property and Invoke method:

            delegate void AddListItemCallback(ListViewItem item);

            private void AddListItem(ListViewItem item)
            {
            if (this.ListView1.InvokeRequired) {
            AddListItemCallback cb = new AddListItemCallback(AddListItem);
            this.ListView1.Invoke(cb, new object[] { item } );
            } else {
            this.Items.Add(item);
            }
            }

            InvokeRequired returns false if the calling thread is the same thread as the one the control was created in, and true otherwise.

            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