简体   繁体   中英

How to update C# wpf ComboBox from Socket.BeginRecieve?

I have a wpf ComboBox databound to an ObservableCollection that needs to be updated from an asynchronous function BeginRecieve, in the Socket class. I know that when the async function is executed it is in a new thread and you cannot update the main GUI controls through other threads. Can someone please give me a example of how this might be accomplished?

Your help is much appreciated thanks.

You can't change the contents of the collection from another thread, you need to do it on the dispatcher thread. So, instead of this:

collection.Add(item);

Do this:

Dispatcher.Invoke(new Action(() => collection.Add(item)));

Another option is to use a collection that raises the CollectionChanged event on the dispatcher thread. I posted an example here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM