SendMessage
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);
}
}
}
}
SendMessage的更多相关文章
- C#调用SendMessage 用法
函数功能:该函数将指定的消息发送到一个或多个窗口.此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回.该函数是应用程序和应用程序之间进行消息传递的主要手段之一. 函数原型:LRESUL ...
- Handler sendMessage 与 obtainMessage (sendToTarget)比较
转自:http://iaiai.iteye.com/blog/1992196 obtainmessage()是从消息池中拿来一个msg 不需要另开辟空间new new需要重新申请,效率低,obtian ...
- Handler.sendMessage 与 Handler.obtainMessage.sendToTarget比较
原文地址: http://www.cnblogs.com/android007/archive/2012/05/10/2494766.html 话说在工作中第一次接触android 的Handler ...
- winform窗体之间通过 windows API SendMessage函数传值
-----------------------------------------------------------‘接收窗体’代码.cs------------------------------ ...
- [C#.net]PostMessage与SendMessage的区别
用 PostMessage.SendNotifyMessage.SendMessageCallback 等异步函数发送系统消息时,参数里不可以使用指针,因为发送者并不等待消息的处理就返回,接受者还没处 ...
- [C#.net] SendMessage
函数功能:该函数将指定的消息发送到一个或多个窗口.此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回.该函数是应用程序和应用程序之间进行消息传递的主要手段之一. 函数原型:LRESU ...
- android wifi obtainmessage sendmessage解析
obtainmessage 从message pool获取一个对象 sendmessage 将message插入message queue java中wait和notify是一对,wait进入睡眠等待 ...
- 【转】在C#中使用SendMessage
SendMessage是一个在user32.dll中声明的API函数,在C#中导入如下: using System.Runtime.InteropServices; [DllImport(" ...
- [外挂6]在指定位置下棋 SendMessage函数
a.鼠标软件模拟,函数SendMessage b.分析窗口内棋子相对坐标X,Y c.软件模拟点击棋盘坐标x,y处的棋子 ::SendMessage(hwnd,WM_LBUTTOMDOWN,0,YX); ...
- C_中使用SendMessage
SendMessage是一个在user32.dll中声明的API函数,在C#中导入如下: using System.Runtime.InteropServices; [DllImport(" ...
随机推荐
- sql to_char 日期转换字符串
1.转换函数 与date操作关系最大的就是两个转换函数:to_date(),to_char() to_date() 作用将字符类型按一定格式转化为日期类型: 具体用法:to_date('2004-11 ...
- iPhone6的CSS3媒体查询
@media only screen and (min-device-width: 375px) and (max-device-width: 667px) and (orientation : po ...
- [转载] Can't create table './store/#sql-b2c_1a.frm' (errno: 150)和sql execution error #1452添加外键时错误解决方法
Can't create table './store/#sql-b2c_1a.frm' (errno: 150)解决方法 错误原因有四: 1.外键的引用类型不一样,主键是int外键是char 2.找 ...
- Android课程---Activity的跳转与传值(转自网上)
Activity跳转与传值,主要是通过Intent类来连接多个Activity,以及传递数据. Intent是Android一个很重要的类.Intent直译是“意图”,什么是意图呢?比如你想从这个 ...
- php课程---随机数
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- VMware网络设置
Host-only:主机想和虚拟机通信时使用 NAT :虚拟机想通过主机IP上网时使用 Bridged:虚拟机直接连接到物理网络时使用 Q:如果主机想和虚拟机正常通信又想能上网怎么办? A:添加两个网 ...
- StringBuffer与StringBuilder有什么区别
package String比较; /* * StringBuffer与StringBuilder有什么区别 * StringBuilder是JDK5增加的一个新类,功能几乎与StringBuffer ...
- 动画 CABasicAnimation animationWithKeyPath 一些规定的值
CABasicAnimation animationWithKeyPath Types When using the ‘CABasicAnimation’ from the QuartzCore Fr ...
- asp.net页面间传值方式
使用asp.net开发项目,必然会在页面间进行传值,本文介绍几种常见的页面传值方式,仅作笔记,以便后续查找使用. 前提:新建两个页面:ValuePage.aspx,ObtainValue.aspx,本 ...
- Java 类加载机制
类的加载: 类的初始化: 类什么时候才被初始化:1)创建类的实例,也就是new一个对象2)访问某个类或接口的静态变量,或者对该静态变量赋值3)调用类的静态方法4)反射(Class.forName(&q ...