using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Diagnostics; namespace Manager.Common
{
public enum EngineResult
{
Success,
FaildAndSuspend,
FaildWithoutSuspend
} //消息传递引擎
public class RelayEngine<T>
{
private Thread _RelayThread;
private AutoResetEvent _ItemArriveEvent = new AutoResetEvent(false);
private ManualResetEvent _ResumeEvent = new ManualResetEvent(true);
private WaitHandle[] _WaitHandles;
private bool _Stop = false; private LinkedList<T> _Buffer = new LinkedList<T>();
private Func<T, bool> _RelayFunc;
private Func<T, EngineResult> _RelayFunc2;
private Action<Exception> _HandleException;
public bool IsSuspend = true; public RelayEngine(Func<T, bool> relayFunc, Action<Exception> handleException, Func<T, EngineResult> relayFunc2 = null)
{
this._WaitHandles = new WaitHandle[] { this._ItemArriveEvent, this._ResumeEvent };
this._RelayFunc = relayFunc;
this._RelayFunc2 = relayFunc2;
this._HandleException = handleException;
this._RelayThread = new Thread(this.Run) { IsBackground = true };
this._RelayThread.Start();
this.IsSuspend = false;
} public void AddItem(T item)
{
lock (this)
{
this._Buffer.AddLast(item);
}
this._ItemArriveEvent.Set();
} public void Suspend()
{
this.IsSuspend = true;
this._ResumeEvent.Reset();
} public void Resume()
{
this.IsSuspend = false;
this._ResumeEvent.Set();
} public void Stop()
{
this.IsSuspend = true; //线程挂起
this._Stop = true; //线程停止
this._ItemArriveEvent.Set();
this._ResumeEvent.Set();
} private void Run()
{
try
{
while (true)
{
if (this._Buffer.Count == )
{
WaitHandle.WaitAll(this._WaitHandles);
}
else
{
this._ResumeEvent.WaitOne(); //队列没有消息阻塞线程,知道收到信号
} if (this._Stop) break; if (this._Buffer.Count > )
{
T item = this._Buffer.First.Value; //先进先出
EngineResult result;
if (this._RelayFunc2 == null)
{
result = this._RelayFunc(item) ? EngineResult.Success : EngineResult.FaildAndSuspend;
}
else
{
result = this._RelayFunc2(item);
}
if (result == EngineResult.Success)
{
lock (this)
{
this._Buffer.RemoveFirst();
}
}
else
{
if (result == EngineResult.FaildAndSuspend) this.Suspend();
}
}
}
}
catch (Exception ex)
{
this._HandleException(ex);
}
}
}
}

最新文章

  1. 无线路由!RTS DTIM阈值、Beacon 周期如何设置多少可以加快路由
  2. UTL_FILE建文件失败“ORA-29280: 目录路径无效”错误
  3. 关于display的那些事儿!
  4. Silverlight中本地化的实现(语言切换)
  5. Node.js V0.12 新特性之性能优化
  6. ubuntu apt-get 遇到的问题
  7. regular expression tutorial
  8. perl-cgi命令行调试
  9. PYTHON基础-入门
  10. C#7.0&amp;6.0新特性 — 完整版
  11. Scrapy实战篇(三)之爬取豆瓣电影短评
  12. SSM框架下使用websocket实现后端发送消息至前端
  13. Android事件处理第一节(View对Touch事件的处理)
  14. javascript 常用的正则表达式验证表单
  15. Ubuntu-18.04设置开机启动脚本
  16. 弹出序列(python)
  17. Spark记录-Scala语法基础
  18. 线程并发线程安全介绍及java.util.concurrent包下类介绍
  19. OpenCV的配置
  20. 用到的C++标准库

热门文章

  1. Python之路(一)
  2. JS 心得总结
  3. 用Webshell直接杀入内网
  4. WPF中ComboBox绑定数据库自动读取产生数据
  5. javascript平时小例子②(正则表达式验证邮箱)
  6. Oracle三大经典表连接适用情况
  7. Js navigator.onLine 获取设备是否可以上网、连接网络
  8. php课程---练习(发布新闻)
  9. jquery 绑定事件的方法
  10. 任务中使用wget,不保存文件