I am new to multithreading and WPF.
I have an ObservableCollection<RSSFeed>
, when adding applications to this collection they are added from the UI thread. RSSFeed properties are associated with the WPF ListView. Later I want to update each RSSFeed asynchronously. So I'm going to implement something like RSSFeed.FetchAsync()
and raise the PropertyChanged property in its updated properties.
I know that ObservableCollection does not support updates from threads other than the UI thread, it throws a NotSupportedException. But since I am not manipulating the ObservableCollection itself, but rather updating the properties of my elements, can I expect this to work and see the updated ListView elements? Or would it still rule out due to PropertyChanged?
Edit: code
RSSFeed.cs
public class RSSFeed { public String Title { get; set; } public String Summary { get; set; } public String Uri { get; set; } public String Encoding { get; set; } public List<FeedItem> Posts { get; set; } public bool FetchedSuccessfully { get; protected set; } public RSSFeed() { Posts = new List<FeedItem>(); } public RSSFeed(String uri) { Posts = new List<FeedItem>(); Uri = uri; Fetch(); } public void FetchAsync() {
UserProfile.cs
public class UserProfile : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public event CollectionChangeEventHandler CollectionChanged; private ObservableCollection<RSSFeed> feeds; public ObservableCollection<RSSFeed> Feeds { get { return feeds; } set { feeds = value; OnPropertyChanged("Feeds"); } } public UserProfile() { feeds = new ObservableCollection<RSSFeed>(); } protected void OnPropertyChanged(string name) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(name)); } } protected void OnCollectionChanged(RSSFeed feed) { CollectionChangeEventHandler handler = CollectionChanged; if (handler != null) { handler(this, new CollectionChangeEventArgs(CollectionChangeAction.Add, feed)); } } }
MainWindow.xaml.cs
public partial class MainWindow : Window, INotifyPropertyChanged {
Thanks.
source share