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(" ...
随机推荐
- mysql中一些简单但是新手容易犯的错误
一.概述 本人近期使用mysql,由于是新手,常常碰到一些问题,因此,在这里做了一个错误备忘录. 二.错误罗列 1.MySQL 记录不存在时插入 记录存在则更新的实现方法 http://www.cnb ...
- [LintCode] Minimum Size Subarray Sum 最小子数组和的大小
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- [LintCode] Add and Search Word 添加和查找单词
Design a data structure that supports the following two operations: addWord(word) and search(word) s ...
- HDU3177 贪心
Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- map的用法
Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!1. map最基本的构造函数: map<stri ...
- bootstrap使用性能问题
1.如icheckbox等的初始化,不要采用类似for in的循环来对所需应用的元素进行初始化,直接采用如 $('[data-toggle="popover"]').popove ...
- SpringMVC如何接收application/json内容编码类型的参数?
在上代码之前,有必要先说说@ResquestBody注解的含义: 1.官方解释如下: Annotation indicating a method parameter should be bound ...
- jquery ui bootstrap日期插件
http://blog.csdn.net/php_897721669/article/details/7404527 搜索“jquery ui日期插件怎么显示年份”? $("#datepic ...
- html 抽奖代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 一个简单驱动的makefile
KVERS = $(shell uname -r) #Kernel modulesobj-m += hello.o build: kernel_modules kernel_modules: make ...