andrew4582

Thread Safe Observable Collection

Dec 31st, 2013
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.37 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections.ObjectModel;
  5. using System.Windows.Threading;
  6. using System.Threading;
  7.  
  8. namespace System.Collections.ObjectModel
  9. {
  10.     public class ThreadSafeObservableCollection<T> : ObservableCollection<T>
  11.     {
  12.  
  13.         Dispatcher _dispatcher;
  14.         ReaderWriterLock _lock;
  15.         public ThreadSafeObservableCollection()
  16.         {
  17.             _dispatcher = Dispatcher.CurrentDispatcher;
  18.             _lock = new ReaderWriterLock();
  19.         }
  20.  
  21.         protected override void ClearItems()
  22.         {
  23.             if (_dispatcher.CheckAccess())
  24.             {
  25.                 LockCookie c = _lock.UpgradeToWriterLock(-1);
  26.                 base.ClearItems();
  27.                 _lock.DowngradeFromWriterLock(ref c);
  28.             }
  29.             else
  30.             {
  31.                 _dispatcher.Invoke(DispatcherPriority.DataBind, (SendOrPostCallback)delegate { Clear(); },null);
  32.             }
  33.         }
  34.         protected override void InsertItem(int index, T item)
  35.         {
  36.             if (_dispatcher.CheckAccess())
  37.             {
  38.                 if (index > this.Count)
  39.                     return;
  40.                 LockCookie c = _lock.UpgradeToWriterLock(-1);
  41.                 base.InsertItem(index, item);
  42.                 _lock.DowngradeFromWriterLock(ref c);
  43.             }
  44.             else
  45.             {
  46.                 object[] e = new object[] { index, item };
  47.                 _dispatcher.Invoke(DispatcherPriority.DataBind, (SendOrPostCallback)delegate { InsertItemImpl(e); }, e);
  48.             }
  49.         }
  50.         void InsertItemImpl(object[] e)
  51.         {
  52.             if (_dispatcher.CheckAccess())
  53.             {
  54.                 InsertItem((int)e[0], (T)e[1]);
  55.             }
  56.             else
  57.             {
  58.                 _dispatcher.Invoke(DispatcherPriority.DataBind, (SendOrPostCallback)delegate { InsertItemImpl(e); });
  59.             }
  60.         }
  61.         protected override void MoveItem(int oldIndex, int newIndex)
  62.         {
  63.             if (_dispatcher.CheckAccess())
  64.             {
  65.                 if (oldIndex >= this.Count | newIndex >= this.Count | oldIndex == newIndex)
  66.                     return;
  67.                 LockCookie c = _lock.UpgradeToWriterLock(-1);
  68.                 base.MoveItem(oldIndex, newIndex);
  69.                 _lock.DowngradeFromWriterLock(ref c);
  70.             }
  71.             else
  72.             {
  73.                 object[] e = new object[] { oldIndex, newIndex };
  74.                 _dispatcher.Invoke(DispatcherPriority.DataBind, (SendOrPostCallback)delegate { MoveItemImpl(e); }, e);
  75.             }
  76.         }
  77.         void MoveItemImpl(object[] e)
  78.         {
  79.             if (_dispatcher.CheckAccess())
  80.             {
  81.                 MoveItem((int)e[0], (int)e[1]);
  82.             }
  83.             else
  84.             {
  85.                 _dispatcher.Invoke(DispatcherPriority.DataBind, (SendOrPostCallback)delegate { MoveItemImpl(e); });
  86.             }
  87.         }
  88.         protected override void RemoveItem(int index)
  89.         {
  90.             if (_dispatcher.CheckAccess())
  91.             {
  92.                 if (index >= this.Count)
  93.                     return;
  94.                 LockCookie c = _lock.UpgradeToWriterLock(-1);
  95.                 base.RemoveItem(index);
  96.                 _lock.DowngradeFromWriterLock(ref c);
  97.             }
  98.             else
  99.             {
  100.                 _dispatcher.Invoke(DispatcherPriority.DataBind, (SendOrPostCallback)delegate { RemoveItem(index); }, index);
  101.             }
  102.         }
  103.         protected override void SetItem(int index, T item)
  104.         {
  105.             if (_dispatcher.CheckAccess())
  106.             {
  107.                 LockCookie c = _lock.UpgradeToWriterLock(-1);
  108.                 base.SetItem(index, item);
  109.                 _lock.DowngradeFromWriterLock(ref c);
  110.             }
  111.             else
  112.             {
  113.                 object[] e = new object[] { index, item };
  114.                 _dispatcher.Invoke(DispatcherPriority.DataBind, (SendOrPostCallback)delegate { SetItemImpl(e); }, e);
  115.             }
  116.         }
  117.         void SetItemImpl(object[] e)
  118.         {
  119.             if (_dispatcher.CheckAccess())
  120.             {
  121.                 SetItem((int)e[0], (T)e[1]);
  122.             }
  123.             else
  124.             {
  125.                 _dispatcher.Invoke(DispatcherPriority.DataBind, (SendOrPostCallback)delegate { SetItemImpl(e); });
  126.             }
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment