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的更多相关文章

  1. C#调用SendMessage 用法

    函数功能:该函数将指定的消息发送到一个或多个窗口.此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回.该函数是应用程序和应用程序之间进行消息传递的主要手段之一.    函数原型:LRESUL ...

  2. Handler sendMessage 与 obtainMessage (sendToTarget)比较

    转自:http://iaiai.iteye.com/blog/1992196 obtainmessage()是从消息池中拿来一个msg 不需要另开辟空间new new需要重新申请,效率低,obtian ...

  3. Handler.sendMessage 与 Handler.obtainMessage.sendToTarget比较

    原文地址: http://www.cnblogs.com/android007/archive/2012/05/10/2494766.html 话说在工作中第一次接触android 的Handler ...

  4. winform窗体之间通过 windows API SendMessage函数传值

    -----------------------------------------------------------‘接收窗体’代码.cs------------------------------ ...

  5. [C#.net]PostMessage与SendMessage的区别

    用 PostMessage.SendNotifyMessage.SendMessageCallback 等异步函数发送系统消息时,参数里不可以使用指针,因为发送者并不等待消息的处理就返回,接受者还没处 ...

  6. [C#.net] SendMessage

    函数功能:该函数将指定的消息发送到一个或多个窗口.此函数为指定的窗口调用窗口程序,直到窗口程序处理完消息再返回.该函数是应用程序和应用程序之间进行消息传递的主要手段之一.     函数原型:LRESU ...

  7. android wifi obtainmessage sendmessage解析

    obtainmessage 从message pool获取一个对象 sendmessage 将message插入message queue java中wait和notify是一对,wait进入睡眠等待 ...

  8. 【转】在C#中使用SendMessage

    SendMessage是一个在user32.dll中声明的API函数,在C#中导入如下: using System.Runtime.InteropServices; [DllImport(" ...

  9. [外挂6]在指定位置下棋 SendMessage函数

    a.鼠标软件模拟,函数SendMessage b.分析窗口内棋子相对坐标X,Y c.软件模拟点击棋盘坐标x,y处的棋子 ::SendMessage(hwnd,WM_LBUTTOMDOWN,0,YX); ...

  10. C_中使用SendMessage

    SendMessage是一个在user32.dll中声明的API函数,在C#中导入如下: using System.Runtime.InteropServices; [DllImport(" ...

随机推荐

  1. session/SessionFactory线程非安全和线程安全

    SessionFactory负责创建session,SessionFactory是线程安全的,多个并发线程可以同时访问一个 SessionFactory 并从中获取Session实例. (Sessio ...

  2. android中versionCode&versionName

    原文来自:http://blog.csdn.net/wh_19910525/article/details/8660416 ,略有修改 一.概述 Android的版本可以在androidmainfes ...

  3. *HDU 1286,2824欧拉函数

    #include<iostream> #include<string> #include<cstdio> #include<cmath> #includ ...

  4. BootStrap 模态框基本用法

    <!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - 模态框(Modal)插件方法</title ...

  5. SpringMVC如何接收application/json内容编码类型的参数?

    在上代码之前,有必要先说说@ResquestBody注解的含义: 1.官方解释如下: Annotation indicating a method parameter should be bound ...

  6. bootstrap input框清空

    <!DOCTYPE HTML> <html> <head> <link href="http://netdna.bootstrapcdn.com/t ...

  7. 字符串格式化命令 sprintf

    原型 int sprintf( char *buffer, const char *format, [ argument] … ); 参数列表 buffer:char型指针,指向将要写入的字符串的缓冲 ...

  8. 使用Sublime Text 直接运行Quick-cocos2d-x 项目

    一.新建一个编译系统 { "cmd": "D:/WorkSoftWare/Quick/quick-cocos2d-x-3.3rc0/quick/samples/Runni ...

  9. 【iCore3 双核心板】例程五:SYSTICK定时器实验——定时点亮LED

    实验指导书及代码包下载: http://pan.baidu.com/s/1eQsKcEY iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...

  10. 【iCore3 双核心板_ uC/OS-III】例程六:信号量——共享资源

    实验指导书及代码包下载: http://pan.baidu.com/s/1milKoVA iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...