首次上传
This commit is contained in:
@@ -0,0 +1,267 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace Sicentury.Core.Collections
|
||||
{
|
||||
/// <summary>
|
||||
/// The collection is thread-safe.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class DelayedPresentRollingObservableCollection<T> : IEnumerable<T>, INotifyCollectionChanged, INotifyPropertyChanged
|
||||
{
|
||||
#region Variables
|
||||
|
||||
public event NotifyCollectionChangedEventHandler CollectionChanged;
|
||||
|
||||
private readonly List<T> _list;
|
||||
private readonly List<T> _cache;
|
||||
|
||||
private readonly object _lockList;
|
||||
private readonly object _lockCache;
|
||||
|
||||
private readonly SemaphoreSlim _semListEnumLocker;
|
||||
|
||||
private readonly int _presentDelayMillisec = 200; // the default delay is 200ms
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
public DelayedPresentRollingObservableCollection(int capacity)
|
||||
{
|
||||
if (capacity <= 0)
|
||||
throw new ArgumentException("the capacity must be greate then 0.", nameof(capacity));
|
||||
|
||||
_lockList = new object();
|
||||
_lockCache = new object();
|
||||
_semListEnumLocker = new SemaphoreSlim(0, 1);
|
||||
|
||||
Capacity = capacity;
|
||||
|
||||
lock (_lockList)
|
||||
{
|
||||
_list = new List<T>();
|
||||
}
|
||||
|
||||
lock (_lockCache)
|
||||
{
|
||||
_cache = new List<T>();
|
||||
}
|
||||
|
||||
_semListEnumLocker.Release();
|
||||
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
Thread.Sleep(2000);
|
||||
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
Present();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Trace.WriteLine($"Unable to present the items, {e.Message}");
|
||||
Debugger.Break();
|
||||
}
|
||||
finally
|
||||
{
|
||||
// present the data every 200ms
|
||||
await Task.Delay(_presentDelayMillisec);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public DelayedPresentRollingObservableCollection(int capacity, int presentDelayMillisec) : this(capacity)
|
||||
{
|
||||
_presentDelayMillisec = presentDelayMillisec < 50 ? 50 : presentDelayMillisec;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
public int Capacity { get; }
|
||||
|
||||
public int Count
|
||||
{
|
||||
get
|
||||
{
|
||||
lock (_lockList)
|
||||
{
|
||||
return _list?.Count ?? 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <summary>
|
||||
/// Copy the items from the cache to the presented list and update the UI.
|
||||
/// </summary>
|
||||
private void Present()
|
||||
{
|
||||
var itemsChanged = new List<T>();
|
||||
|
||||
lock (_lockCache)
|
||||
{
|
||||
if (_cache.Count <= 0)
|
||||
return;
|
||||
|
||||
itemsChanged.AddRange(_cache);
|
||||
_cache.Clear();
|
||||
}
|
||||
|
||||
var notifyAction = NotifyCollectionChangedAction.Add;
|
||||
lock (_lockList)
|
||||
{
|
||||
_list.AddRange(itemsChanged);
|
||||
|
||||
if (_list.Count >= Capacity)
|
||||
{
|
||||
var cntToRemain = Capacity / 2;
|
||||
_list.RemoveRange(0, _list.Count - cntToRemain);
|
||||
|
||||
notifyAction = NotifyCollectionChangedAction.Reset;
|
||||
}
|
||||
}
|
||||
|
||||
// raise the collection changed event.
|
||||
SynchronizationContext.SetSynchronizationContext(
|
||||
new DispatcherSynchronizationContext(Application.Current.Dispatcher));
|
||||
|
||||
SynchronizationContext.Current.Post(p1 =>
|
||||
{
|
||||
CollectionChanged?.Invoke(
|
||||
this,
|
||||
new NotifyCollectionChangedEventArgs(notifyAction,
|
||||
notifyAction == NotifyCollectionChangedAction.Add ? itemsChanged : null));
|
||||
}, null);
|
||||
|
||||
SynchronizationContext.Current.Post(p1 =>
|
||||
{
|
||||
OnPropertyChanged(nameof(Count));
|
||||
}, null);
|
||||
}
|
||||
|
||||
public void Flush()
|
||||
{
|
||||
Present();
|
||||
|
||||
// raise the collection changed event.
|
||||
SynchronizationContext.SetSynchronizationContext(
|
||||
new DispatcherSynchronizationContext(Application.Current.Dispatcher));
|
||||
|
||||
SynchronizationContext.Current.Post(p1 =>
|
||||
{
|
||||
CollectionChanged?.Invoke(
|
||||
this,
|
||||
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
|
||||
}, null);
|
||||
|
||||
SynchronizationContext.Current.Post(p1 =>
|
||||
{
|
||||
OnPropertyChanged(nameof(Count));
|
||||
}, null);
|
||||
}
|
||||
|
||||
public void Add(T newItem)
|
||||
{
|
||||
lock (_lockCache)
|
||||
{
|
||||
_cache.Add(newItem);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Remove(T oldItem)
|
||||
{
|
||||
lock (_lockList)
|
||||
{
|
||||
return _list.Remove(oldItem);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveAt(int index)
|
||||
{
|
||||
lock (_lockList)
|
||||
{
|
||||
_list.RemoveAt(index);
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveRange(int index, int count)
|
||||
{
|
||||
lock (_lockList)
|
||||
{
|
||||
_list.RemoveRange(index, count);
|
||||
}
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
lock (_lockCache)
|
||||
{
|
||||
_cache.Clear();
|
||||
}
|
||||
|
||||
lock (_lockList)
|
||||
{
|
||||
_list.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
lock (_lockList)
|
||||
{
|
||||
_semListEnumLocker.Wait();
|
||||
return new SafeEnumerator<T>(_list.GetEnumerator(), _semListEnumLocker);
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
lock (_lockList)
|
||||
{
|
||||
_semListEnumLocker.Wait();
|
||||
return new SafeEnumerator<T>(_list.GetEnumerator(), _semListEnumLocker);
|
||||
}
|
||||
}
|
||||
|
||||
public T this[int index]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (index < 0 || index >= Count)
|
||||
throw new ArgumentOutOfRangeException();
|
||||
|
||||
lock (_lockList)
|
||||
{
|
||||
return _list[index];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public event PropertyChangedEventHandler PropertyChanged;
|
||||
|
||||
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
|
||||
{
|
||||
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
|
||||
namespace Sicentury.Core.Collections
|
||||
{
|
||||
public class ObservableQueue<T> : Queue<T>, INotifyCollectionChanged
|
||||
{
|
||||
public event NotifyCollectionChangedEventHandler CollectionChanged;
|
||||
|
||||
private readonly object _syncRoot = new object();
|
||||
|
||||
#region Constructors
|
||||
|
||||
public ObservableQueue()
|
||||
{
|
||||
LimitSize = 0;
|
||||
}
|
||||
|
||||
public ObservableQueue(int capacity) : base(capacity)
|
||||
{
|
||||
LimitSize = capacity;
|
||||
}
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties
|
||||
|
||||
public int LimitSize { get; }
|
||||
|
||||
public new virtual void Enqueue(T obj)
|
||||
{
|
||||
lock (_syncRoot)
|
||||
{
|
||||
base.Enqueue(obj);
|
||||
|
||||
if (LimitSize > 0 && base.Count > LimitSize)
|
||||
base.Dequeue();
|
||||
|
||||
CollectionChanged?.Invoke(this,
|
||||
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, obj));
|
||||
}
|
||||
}
|
||||
|
||||
public new virtual T Dequeue()
|
||||
{
|
||||
lock (_syncRoot)
|
||||
{
|
||||
var obj = base.Dequeue();
|
||||
CollectionChanged?.Invoke(this,
|
||||
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, obj));
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public new virtual void Clear()
|
||||
{
|
||||
lock (_syncRoot)
|
||||
{
|
||||
base.Clear();
|
||||
CollectionChanged?.Invoke(this,
|
||||
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Specialized;
|
||||
using System.Linq;
|
||||
|
||||
namespace Sicentury.Core.Collections
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public class ObservableRangeCollection<T> : ObservableCollection<T>
|
||||
{
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class.
|
||||
/// </summary>
|
||||
public ObservableRangeCollection()
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the System.Collections.ObjectModel.ObservableCollection(Of T) class that contains elements copied from the specified collection.
|
||||
/// </summary>
|
||||
/// <param name="collection">collection: The collection from which the elements are copied.</param>
|
||||
/// <exception cref="System.ArgumentNullException">The collection parameter cannot be null.</exception>
|
||||
public ObservableRangeCollection(IEnumerable<T> collection)
|
||||
: base(collection)
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Adds the elements of the specified collection to the end of the ObservableCollection(Of T).
|
||||
/// </summary>
|
||||
public virtual void AddRange(IEnumerable<T> collection)
|
||||
{
|
||||
if (collection == null) throw new ArgumentNullException(nameof(collection));
|
||||
|
||||
var startIndex = Count;
|
||||
var enumerable = collection.ToList();
|
||||
foreach (var i in enumerable)
|
||||
Items.Add(i);
|
||||
|
||||
OnCollectionChanged(
|
||||
new NotifyCollectionChangedEventArgs(
|
||||
NotifyCollectionChangedAction.Reset));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the first occurence of each item in the specified collection from ObservableCollection(Of T).
|
||||
/// </summary>
|
||||
public virtual void RemoveRange(IEnumerable<T> collection)
|
||||
{
|
||||
if (collection == null) throw new ArgumentNullException(nameof(collection));
|
||||
|
||||
foreach (var i in collection)
|
||||
Items.Remove(i);
|
||||
|
||||
OnCollectionChanged(
|
||||
new NotifyCollectionChangedEventArgs(
|
||||
NotifyCollectionChangedAction.Reset));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears the current collection and replaces it with the specified item.
|
||||
/// </summary>
|
||||
public virtual void Replace(T item)
|
||||
{
|
||||
ReplaceRange(new[] {item});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clears the current collection and replaces it with the specified collection.
|
||||
/// </summary>
|
||||
public void ReplaceRange(IEnumerable<T> collection)
|
||||
{
|
||||
if (collection == null) throw new ArgumentNullException(nameof(collection));
|
||||
|
||||
Items.Clear();
|
||||
foreach (var i in collection) Items.Add(i);
|
||||
OnCollectionChanged(
|
||||
new NotifyCollectionChangedEventArgs(
|
||||
NotifyCollectionChangedAction.Reset));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
|
||||
namespace Sicentury.Core.Collections
|
||||
{
|
||||
public sealed class SafeEnumerator<T> : IEnumerator<T>
|
||||
|
||||
{
|
||||
#region variables
|
||||
|
||||
// this is the (thread-unsafe)
|
||||
// enumerator of the underlying collection
|
||||
private readonly IEnumerator<T> _enumeratorInner;
|
||||
|
||||
// this is the object we shall lock on.
|
||||
private readonly SemaphoreSlim _semLocker;
|
||||
|
||||
#endregion
|
||||
|
||||
public SafeEnumerator(IEnumerator<T> inner, SemaphoreSlim semLocker)
|
||||
{
|
||||
// entering lock in constructor
|
||||
//semLocker.Wait();
|
||||
_semLocker = semLocker;
|
||||
|
||||
_enumeratorInner = inner;
|
||||
}
|
||||
|
||||
#region Implementation of IDisposable
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_semLocker.Release();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Implementation of IEnumerator
|
||||
|
||||
// we just delegate actual implementation
|
||||
// to the inner enumerator, that actually iterates
|
||||
// over some collection
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
return _enumeratorInner.MoveNext();
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_enumeratorInner.Reset();
|
||||
}
|
||||
|
||||
public T Current => _enumeratorInner.Current;
|
||||
|
||||
object IEnumerator.Current => Current;
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user