Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //...
- public ObservableCollection<object> SourceObservableCollection { get { return (ObservableCollection<object>)SourceCollection; } }
- //<Part of Attempt7>
- protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
- {
- base.OnCollectionChanged(args);
- isCollectionChanging = false;
- }
- private bool isCollectionChanging = false;
- //</Part of Attempt7>
- //<Part of Attempt9>
- private static readonly object _lock = new object();
- //</Part of Attempt9>
- //<*async syntax is part of Attempt10*/>
- public async void RefreshSource()
- {
- SourceObservableCollection.Clear();
- // refreshSourceFunction retrieves data from Database
- IEnumerable result = refreshSourceFunction(/*parameters*/);
- ////Attempt1:
- foreach (object item in result)
- {
- SourceObservableCollection.Add(item);
- }
- ////Attempt2:
- //foreach (object item in result.OfType<object>().ToList())
- //{
- // SourceObservableCollection.Add(item);
- //}
- ////Attempt3:
- //List<object> lstResult = result.OfType<object>().ToList();
- //foreach (object item in lstResult)
- // SourceObservableCollection.Add(item);
- ////Attempt4:
- //List<object> lstResult2 = result.OfType<object>().ToList();
- //for (int x = 0; x < lstResult2.Count; x++)
- //{
- // SourceObservableCollection.Add(lstResult2[x]);
- //}
- ////Attempt5:
- //IEnumerator enumerator = result.GetEnumerator();
- //while (enumerator.MoveNext())
- //{
- // SourceObservableCollection.Add(enumerator.Current);
- //}
- ////Attempt6:
- //IEnumerator enumerator2 = result.GetEnumerator();
- //while (enumerator2.MoveNext())
- //{
- // Dispatcher.Invoke(() =>
- // {
- // SourceObservableCollection.Add(enumerator2.Current);
- // });
- //}
- ////Attempt7:
- //foreach (object item in result)
- //{
- // isCollectionChanging = true;
- // Dispatcher.Invoke(() =>
- // {
- // SourceObservableCollection.Add(item);
- // }, System.Windows.Threading.DispatcherPriority.Background);
- // while (isCollectionChanging) ;
- //}
- ////Attempt8:
- //foreach (object item in result)
- //{
- // SourceObservableCollection.Add(item);
- // Refresh();
- //}
- ////Attempt9:
- //foreach (object item in result)
- //{
- // lock (_lock)
- // {
- // SourceObservableCollection.Add(item);
- // }
- //}
- ////Attempt10:
- //await Dispatcher.InvokeAsync(() => SourceObservableCollection.Clear());
- //IEnumerable result2 = await Task.Run(() => refreshSourceFunction(/*parameters*/));
- //foreach (object item in result2)
- //{
- // await Dispatcher.InvokeAsync(() => SourceObservableCollection.Add(item));
- //}
- }
- //...
- /// <summary>
- /// for a collection implementing IEnumerable this offers
- /// optimistic indexer, i.e. this[int index] { get; }
- /// and cached Count/IsEmpty properties and IndexOf method,
- /// assuming that after an initial request to read item[N],
- /// the following indices will be a sequence for index N+1, N+2 etc.
- /// </summary>
- /// <remarks>
- /// This class is NOT safe for multi-threaded use.
- /// if the source collection implements IList or ICollection, the corresponding
- /// properties/methods will be used instead of the cached versions
- /// </remarks>
- internal class IndexedEnumerable : IEnumerable, IWeakEventListener
- {
- //...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement