-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathSecurityQueue.cs
168 lines (142 loc) · 4.11 KB
/
SecurityQueue.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SecurityQueue.cs" company="pzcast">
// (C) 2015 pzcast. All rights reserved.
// </copyright>
// <summary>
// The security queue.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace SimpleCrawler
{
using System.Collections.Generic;
using System.Threading;
/// <summary>
/// The security queue.
/// </summary>
/// <typeparam name="T">
/// Any type.
/// </typeparam>
public abstract class SecurityQueue<T>
where T : class
{
#region Fields
/// <summary>
/// The inner queue.
/// </summary>
protected readonly Queue<T> InnerQueue = new Queue<T>();
/// <summary>
/// The sync object.
/// </summary>
protected readonly object SyncObject = new object();
/// <summary>
/// The auto reset event.
/// </summary>
private readonly AutoResetEvent autoResetEvent;
#endregion
#region Constructors and Destructors
/// <summary>
/// Initializes a new instance of the <see cref="SecurityQueue{T}"/> class.
/// </summary>
protected SecurityQueue()
{
this.autoResetEvent = new AutoResetEvent(false);
}
#endregion
#region Delegates
/// <summary>
/// The before en queue event handler.
/// </summary>
/// <param name="target">
/// The target.
/// </param>
/// <returns>
/// The <see cref="bool"/>.
/// </returns>
public delegate bool BeforeEnQueueEventHandler(T target);
#endregion
#region Public Events
/// <summary>
/// The before en queue event.
/// </summary>
public event BeforeEnQueueEventHandler BeforeEnQueueEvent;
#endregion
#region Public Properties
/// <summary>
/// Gets the auto reset event.
/// </summary>
public AutoResetEvent AutoResetEvent
{
get
{
return this.autoResetEvent;
}
}
/// <summary>
/// Gets the count.
/// </summary>
public int Count
{
get
{
lock (this.SyncObject)
{
return this.InnerQueue.Count;
}
}
}
/// <summary>
/// Gets a value indicating whether has value.
/// </summary>
public bool HasValue
{
get
{
return this.Count != 0;
}
}
#endregion
#region Public Methods and Operators
/// <summary>
/// The de queue.
/// </summary>
/// <returns>
/// The <see cref="T"/>.
/// </returns>
public T DeQueue()
{
lock (this.SyncObject)
{
if (this.InnerQueue.Count > 0)
{
return this.InnerQueue.Dequeue();
}
return default(T);
}
}
/// <summary>
/// The en queue.
/// </summary>
/// <param name="target">
/// The target.
/// </param>
public void EnQueue(T target)
{
lock (this.SyncObject)
{
if (this.BeforeEnQueueEvent != null)
{
if (this.BeforeEnQueueEvent(target))
{
this.InnerQueue.Enqueue(target);
}
}
else
{
this.InnerQueue.Enqueue(target);
}
this.AutoResetEvent.Set();
}
}
#endregion
}
}