wasSharp – Blame information for rev 55
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
55 | office | 1 | using System; |
2 | using System.Threading; |
||
3 | using System.Threading.Tasks; |
||
4 | using System.Collections.ObjectModel; |
||
54 | office | 5 | using System.Collections.Concurrent; |
55 | office | 6 | using System.Collections; |
7 | using System.Collections.Generic; |
||
8 | using System.Collections.Specialized; |
||
9 | using System.Linq; |
||
54 | office | 10 | |
11 | namespace wasSharp.Collections.Specialized |
||
12 | { |
||
13 | /// <summary> |
||
14 | /// The observable concurrent queue. |
||
15 | /// </summary> |
||
16 | /// <typeparam name="T"> |
||
17 | /// The content type |
||
18 | /// </typeparam> |
||
55 | office | 19 | public sealed class ObservableConcurrentQueue<T> : ConcurrentQueue<T>, INotifyCollectionChanged |
54 | office | 20 | { |
55 | office | 21 | public event NotifyCollectionChangedEventHandler CollectionChanged; |
54 | office | 22 | |
55 | office | 23 | private new void Enqueue(T item) |
24 | { |
||
25 | EnqueueAsync(item).RunSynchronously(); |
||
26 | } |
||
54 | office | 27 | |
55 | office | 28 | public async Task EnqueueAsync(T item) => await Task.Run(() => |
54 | office | 29 | { |
30 | base.Enqueue(item); |
||
31 | |||
55 | office | 32 | OnCollectionChanged( |
33 | new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item)); |
||
54 | office | 34 | }); |
35 | |||
55 | office | 36 | private new bool TryDequeue(out T result) |
54 | office | 37 | { |
55 | office | 38 | result = DequeueAsync().Result; |
39 | if (result.Equals(default(T))) |
||
54 | office | 40 | return false; |
41 | |||
55 | office | 42 | return true; |
43 | } |
||
54 | office | 44 | |
55 | office | 45 | public async Task<T> DequeueAsync() => await Task.Run(() => |
54 | office | 46 | { |
55 | office | 47 | if (!base.TryDequeue(out T item)) |
48 | return default(T); |
||
54 | office | 49 | |
55 | office | 50 | OnCollectionChanged( |
51 | new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item)); |
||
52 | |||
53 | return item; |
||
54 | }); |
||
55 | |||
56 | public async Task<T> PeekAsync() => await Task.Run(() => |
||
57 | { |
||
58 | if (!base.TryPeek(out T item)) |
||
59 | return default(T); |
||
60 | |||
61 | return item; |
||
62 | }); |
||
63 | |||
64 | private new bool TryPeek(out T result) |
||
65 | { |
||
66 | result = PeekAsync().Result; |
||
67 | |||
68 | if (result.Equals(default(T))) |
||
69 | return false; |
||
70 | |||
54 | office | 71 | return true; |
72 | } |
||
73 | |||
55 | office | 74 | private void OnCollectionChanged(NotifyCollectionChangedEventArgs args) |
54 | office | 75 | { |
55 | office | 76 | CollectionChanged?.Invoke(this, args); |
54 | office | 77 | } |
78 | |||
79 | |||
55 | office | 80 | public async Task Clear() |
54 | office | 81 | { |
55 | office | 82 | while (!base.IsEmpty) |
83 | await DequeueAsync(); |
||
54 | office | 84 | } |
85 | } |
||
86 | } |