wasSharp – Blame information for rev 54
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
54 | office | 1 | // -------------------------------------------------------------------------------------------------------------------- |
2 | // <copyright file="ObservableConcurrentQueue.cs" company="BledSoft"> |
||
3 | // This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. |
||
4 | // To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/. |
||
5 | // </copyright> |
||
6 | // <Author> |
||
7 | // Cheikh Younes |
||
8 | // </Author> |
||
9 | // -------------------------------------------------------------------------------------------------------------------- |
||
10 | using System.Collections.Concurrent; |
||
11 | using System.Threading.Tasks; |
||
12 | |||
13 | namespace wasSharp.Collections.Specialized |
||
14 | { |
||
15 | /// <summary> |
||
16 | /// The observable concurrent queue. |
||
17 | /// </summary> |
||
18 | /// <typeparam name="T"> |
||
19 | /// The content type |
||
20 | /// </typeparam> |
||
21 | public sealed class ObservableConcurrentQueue<T> : ConcurrentQueue<T> |
||
22 | { |
||
23 | #region Public Events |
||
24 | |||
25 | /// <summary> |
||
26 | /// Occurs when concurrent queue elements [changed]. |
||
27 | /// </summary> |
||
28 | public event ConcurrentQueueChangedEventHandler<T> ContentChanged; |
||
29 | |||
30 | #endregion |
||
31 | |||
32 | #region Public Methods and Operators |
||
33 | |||
34 | /// <summary> |
||
35 | /// Adds an object to the end of the <see cref="T:System.Collections.Concurrent.ConcurrentQueue`1"/>. |
||
36 | /// </summary> |
||
37 | /// <param name="item"> |
||
38 | /// The object to add to the end of the <see cref="T:System.Collections.Concurrent.ConcurrentQueue`1"/> |
||
39 | /// . The value can be a null reference (Nothing in Visual Basic) for reference types. |
||
40 | /// </param> |
||
41 | public new async Task Enqueue(T item) |
||
42 | { |
||
43 | await new Task(() => |
||
44 | { |
||
45 | base.Enqueue(item); |
||
46 | |||
47 | // Raise event added event |
||
48 | this.OnContentChanged( |
||
49 | new NotifyConcurrentQueueChangedEventArgs<T>(NotifyConcurrentQueueChangedAction.Enqueue, item)); |
||
50 | }); |
||
51 | } |
||
52 | |||
53 | /// <summary> |
||
54 | /// Attempts to remove and return the object at the beginning of the |
||
55 | /// <see cref="T:System.Collections.Concurrent.ConcurrentQueue`1"/>. |
||
56 | /// </summary> |
||
57 | /// <param name="result"> |
||
58 | /// When this method returns, if the operation was successful, <paramref name="result"/> contains the |
||
59 | /// object removed. If no object was available to be removed, the value is unspecified. |
||
60 | /// </param> |
||
61 | /// <returns> |
||
62 | /// true if an element was removed and returned from the beginning of the |
||
63 | /// <see cref="T:System.Collections.Concurrent.ConcurrentQueue`1"/> successfully; otherwise, false. |
||
64 | /// </returns> |
||
65 | public new bool TryDequeue(out T result) |
||
66 | { |
||
67 | if (!base.TryDequeue(out result)) |
||
68 | { |
||
69 | return false; |
||
70 | } |
||
71 | |||
72 | // Raise item dequeued event |
||
73 | this.OnContentChanged( |
||
74 | new NotifyConcurrentQueueChangedEventArgs<T>(NotifyConcurrentQueueChangedAction.Dequeue, result)); |
||
75 | |||
76 | if (this.IsEmpty) |
||
77 | { |
||
78 | // Raise Queue empty event |
||
79 | this.OnContentChanged( |
||
80 | new NotifyConcurrentQueueChangedEventArgs<T>(NotifyConcurrentQueueChangedAction.Empty)); |
||
81 | } |
||
82 | |||
83 | return true; |
||
84 | } |
||
85 | |||
86 | /// <summary> |
||
87 | /// Attempts to return an object from the beginning of the |
||
88 | /// <see cref="T:System.Collections.Concurrent.ConcurrentQueue`1"/> without removing it. |
||
89 | /// </summary> |
||
90 | /// <param name="result"> |
||
91 | /// When this method returns, <paramref name="result"/> contains an object from the beginning of the |
||
92 | /// <see cref="T:System.Collections.Concurrent.ConcurrentQueue`1"/> or an unspecified value if the operation failed. |
||
93 | /// </param> |
||
94 | /// <returns> |
||
95 | /// true if and object was returned successfully; otherwise, false. |
||
96 | /// </returns> |
||
97 | public new bool TryPeek(out T result) |
||
98 | { |
||
99 | var retValue = base.TryPeek(out result); |
||
100 | if (retValue) |
||
101 | { |
||
102 | // Raise item dequeued event |
||
103 | this.OnContentChanged( |
||
104 | new NotifyConcurrentQueueChangedEventArgs<T>(NotifyConcurrentQueueChangedAction.Peek, result)); |
||
105 | } |
||
106 | |||
107 | return retValue; |
||
108 | } |
||
109 | |||
110 | #endregion |
||
111 | |||
112 | #region Methods |
||
113 | |||
114 | /// <summary> |
||
115 | /// Raises the <see cref="E:Changed"/> event. |
||
116 | /// </summary> |
||
117 | /// <param name="args"> |
||
118 | /// The <see cref="NotifyConcurrentQueueChangedEventArgs{T}"/> instance containing the event data. |
||
119 | /// </param> |
||
120 | private void OnContentChanged(NotifyConcurrentQueueChangedEventArgs<T> args) |
||
121 | { |
||
122 | var handler = this.ContentChanged; |
||
123 | if (handler != null) |
||
124 | { |
||
125 | handler(this, args); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | #endregion |
||
130 | } |
||
131 | } |