the wait queue
2018-06-17 21:50:14来源:未知 阅读 ()
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(); } } } } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- PackagesNotFoundError: The following packages are not av 2019-07-24
- Windows安装python3.x后,pip list警告!DEPRECATION: The d 2019-04-25
- 小白学习随笔the first week 2019-04-11
- django-rest-framework框架 第四篇 认证Authentication 2019-04-11
- python3.6以上 asyncio模块的异步编程模型 async await语法 2019-02-17
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash