开发者

How to make Object bound to WPF control thread safe [duplicate]

开发者 https://www.devze.com 2023-04-10 00:30 出处:网络
This question already has answers here: WPF Databinding thread safety? (3 answers) Thread safety, lists, binding and WPF
This question already has answers here: WPF Databinding thread safety? (3 answers) Thread safety, lists, binding and WPF (2 answers) Closed 2 years ago.

I have an Observable collection object whose value I am updating from my code. This collection is bound two way to a data grid. Now, is this collection thread safe - that is if I try to modify the value of the collection from the code and at th开发者_C百科e same time the user tries to modify it(as a result of editing the data grid), would the program throw an exception? If yes, can you please explain how to avoid this?

Thanks...


This looks like a duplicate here, and here but the short answer is that you're safe if you're modifying a property.

As you're working with a collection there's a bit more you need to do if you're trying to modify an ObservableCollection on multiple threads and not just the UI thread. This has been covered a lot, and you can check out at either this link or this one

However if you're doing your modifications within the UI thread, you are safe as this is what the ObservableCollection is intended for. The events will be created and handled on the UI theread as long as you are properly marshaling via Dispatcher.BeginInvoke().

0

精彩评论

暂无评论...
验证码 换一张
取 消