Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. //...
  2. public ObservableCollection<object> SourceObservableCollection { get { return (ObservableCollection<object>)SourceCollection; } }
  3.  
  4. //<Part of Attempt7>
  5. protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
  6. {
  7. base.OnCollectionChanged(args);
  8. isCollectionChanging = false;
  9. }
  10. private bool isCollectionChanging = false;
  11. //</Part of Attempt7>
  12. //<Part of Attempt9>
  13. private static readonly object _lock = new object();
  14. //</Part of Attempt9>
  15. //<*async syntax is part of Attempt10*/>
  16. public async void RefreshSource()
  17. {
  18. SourceObservableCollection.Clear();
  19.  
  20. // refreshSourceFunction retrieves data from Database
  21. IEnumerable result = refreshSourceFunction(/*parameters*/);
  22.  
  23. ////Attempt1:
  24. foreach (object item in result)
  25. {
  26. SourceObservableCollection.Add(item);
  27. }
  28.  
  29. ////Attempt2:
  30. //foreach (object item in result.OfType<object>().ToList())
  31. //{
  32. // SourceObservableCollection.Add(item);
  33. //}
  34.  
  35. ////Attempt3:
  36. //List<object> lstResult = result.OfType<object>().ToList();
  37. //foreach (object item in lstResult)
  38. // SourceObservableCollection.Add(item);
  39.  
  40. ////Attempt4:
  41. //List<object> lstResult2 = result.OfType<object>().ToList();
  42. //for (int x = 0; x < lstResult2.Count; x++)
  43. //{
  44. // SourceObservableCollection.Add(lstResult2[x]);
  45. //}
  46.  
  47. ////Attempt5:
  48. //IEnumerator enumerator = result.GetEnumerator();
  49. //while (enumerator.MoveNext())
  50. //{
  51. // SourceObservableCollection.Add(enumerator.Current);
  52. //}
  53.  
  54. ////Attempt6:
  55. //IEnumerator enumerator2 = result.GetEnumerator();
  56. //while (enumerator2.MoveNext())
  57. //{
  58. // Dispatcher.Invoke(() =>
  59. // {
  60. // SourceObservableCollection.Add(enumerator2.Current);
  61. // });
  62. //}
  63.  
  64. ////Attempt7:
  65. //foreach (object item in result)
  66. //{
  67. // isCollectionChanging = true;
  68. // Dispatcher.Invoke(() =>
  69. // {
  70. // SourceObservableCollection.Add(item);
  71. // }, System.Windows.Threading.DispatcherPriority.Background);
  72. // while (isCollectionChanging) ;
  73. //}
  74.  
  75. ////Attempt8:
  76. //foreach (object item in result)
  77. //{
  78. // SourceObservableCollection.Add(item);
  79. // Refresh();
  80. //}
  81.  
  82. ////Attempt9:
  83. //foreach (object item in result)
  84. //{
  85. // lock (_lock)
  86. // {
  87. // SourceObservableCollection.Add(item);
  88. // }
  89. //}
  90.  
  91. ////Attempt10:
  92. //await Dispatcher.InvokeAsync(() => SourceObservableCollection.Clear());
  93. //IEnumerable result2 = await Task.Run(() => refreshSourceFunction(/*parameters*/));
  94. //foreach (object item in result2)
  95. //{
  96. // await Dispatcher.InvokeAsync(() => SourceObservableCollection.Add(item));
  97. //}
  98. }
  99. //...
  100.  
  101. /// <summary>
  102. /// for a collection implementing IEnumerable this offers
  103. /// optimistic indexer, i.e. this[int index] { get; }
  104. /// and cached Count/IsEmpty properties and IndexOf method,
  105. /// assuming that after an initial request to read item[N],
  106. /// the following indices will be a sequence for index N+1, N+2 etc.
  107. /// </summary>
  108. /// <remarks>
  109. /// This class is NOT safe for multi-threaded use.
  110. /// if the source collection implements IList or ICollection, the corresponding
  111. /// properties/methods will be used instead of the cached versions
  112. /// </remarks>
  113. internal class IndexedEnumerable : IEnumerable, IWeakEventListener
  114. {
  115. //...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement