设为首页 加入收藏

TOP

the wait queue
2019-09-03 03:14:39 】 浏览:14
Tags:the wait queue
using System;
using System.Collections.Concurrent;
using System.Threading;

namespace Base
{
    public class WaitQueue<T> : IDisposable where T : class
    {
        /// <summary>
        /// The deal action.
        /// </summary>
        public Action<T> DealAction { get; set; }

        /// <summary>
        /// The inner queue.
        /// </summary>
        private readonly ConcurrentQueue<T> _innerQueue;

        /// <summary>
        /// The deal thread.
        /// </summary>
        private readonly Thread dealThread;

        /// <summary>
        /// The flag for end thread.
        /// </summary>
        private bool endThreadFlag = false;

        /// <summary>
        /// The auto reset event.
        /// </summary>
        private readonly AutoResetEvent autoResetEvent = new AutoResetEvent(true);

        /// <summary>
        /// Initializes a new instance of the WaitQueue`1 class.
        /// </summary>
        public WaitQueue()
        {
            this._innerQueue = new ConcurrentQueue<T>();
            this.dealThread = new Thread(this.DealQueue);
            this.dealThread.Start();
        }

        /// <summary>
        /// Disposes current instance, end the deal thread and inner queue.
        /// </summary>
        public void Dispose()
        {
            this.endThreadFlag = true;
            this._innerQueue.Enqueue(null);
            this.autoResetEvent.Set();
            this.dealThread.Join();
            this.autoResetEvent.Close();
        }

        /// <summary>
        /// Save entity to Queue.
        /// </summary>
        /// <param name="entity">The entity what will be deal.</param>
        public void SaveLog(T entity)
        {
            this._innerQueue.Enqueue(entity);
            this.autoResetEvent.Set();
        }

        /// <summary>
        /// Out Queue.
        /// </summary>
        /// <param name="entity">The init entity.</param>
        /// <returns>The entity what will be deal.</returns>
        private bool Dequeue(out T entity)
        {
            return this._innerQueue.TryDequeue(out entity);
        }

        /// <summary>
        /// Deal entity in Queue.
        /// </summary>
        private void DealQueue()
        {
            while (true)
            {
                T entity;
                if (this.Dequeue(out entity))
                {
                    if (this.endThreadFlag && entity == null)
                    {
                        return;   // Exit the deal thread.
                    }

                    try
                    {
                        if (this.DealAction != null)
                        {
                            this.DealAction(entity);
                        }
                    }
                    catch
                    {
                    }
                }
                else
                {
                    this.autoResetEvent.WaitOne();
                }
            }
        }
    }
}

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Linq to SQL只支持SQL Server(所.. 下一篇Web GIS 离线地图

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目