Thread problem...
-
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?
-
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?
-
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?
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
-
What your experiencing is called Invalid cross-thread operations. You can read more about it here
-
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?
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.