1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Common.system
- {
-
-
-
-
-
-
- public class QueueSync<T>
- {
- int _maxcount = 10000;
- Queue<T> _queue = new Queue<T>();
-
-
-
- public int Count
- {
- get
- {
- if (_queue == null)
- {
- return 0;
- }
- else
- {
- return _queue.Count;
- }
- }
- }
-
-
-
-
- public QueueSync() : this(1000) { }
-
-
-
-
-
- public QueueSync(int maxcount)
- {
-
- _maxcount = maxcount;
- }
-
-
-
-
-
-
- public bool Enqueue(T obj)
- {
- bool result = false;
- lock (this)
- {
- if (_queue.Count > _maxcount)
- {
- _queue.Dequeue();
- }
- _queue.Enqueue(obj);
- result = true;
- }
- return result;
- }
-
-
-
-
- public T Dequeue()
- {
- T obj = default(T);
- lock (this)
- {
- try
- {
- obj = _queue.Dequeue();
- }
- catch
- {
-
- }
- }
- return obj;
- }
- }
- }
|