Replacement for ObservableCollection?

2020-07-23 05:02发布

问题:

I'm looking for a replacement for ObservableCollection that is thread-safe, raises events on the UI thread and preferably has an AddRange method. I've seen a few implementations posted here and elsewhere, but I'd prefer an implementation that has been tested and validated and that I won't have to maintain myself.

So can anyone point me to such a replacement, preferable available via NuGet?

回答1:

For thread safe collections check the System.Collections.Concurrent namespace. ConcurrentStack has a PushRange method that inserts multiple objects at the top of the stack.



回答2:

You could try using the BulkObservableCollection from Microsoft, although I'm not sure why they put it in such a strange namespace! Didn't see it listed when I tried to "Add reference..." in VS2012, had to manually browse to C:\Windows\Microsoft.NET\assembly\GAC_MSIL



回答3:

You can try the one described here:

http://deanchalk.com/2010/02/01/thread-safe-dispatcher-safe-observable-collection-for-wpf/

It is a full implementation in a blog post. It is available as source (from that page), so it might not meet your tested/validated criteria.

I have not used it, so I cannot vouch for how well it works (or doesn't).

UPDATE: The old link was broken. I replaced it with the current link. The Dean Chalk is actually a member here on SO, so here is a link to his SO profile in case it ever breaks again:

https://stackoverflow.com/users/328848/dean-chalk