DataSent PushData(int type, object data, int priority, MsgQueue.MessageProc proc) { List <OneQueue> qs = Receiver.Queues; if (priority < 0 || priority >= qs.Count) { priority = qs.Count - 1; } object o = data; OneQueue.Message msg = new OneQueue.Message(); msg.sender = this; msg.Data = o; msg.type = type; mutex.WaitOne(); qs[priority].PushData(msg); DataSent d = null; if (type == MsgQueue.msgtype_Request) { d = new DataSent(msg); sentMsgs.Add(d); } mutex.ReleaseMutex(); return(d); }
internal void MessageFinished(OneQueue.Message msg, object ret) { mutex.WaitOne(); foreach (DataSent d in sentMsgs) { if (d.msg == msg) { sentMsgs.Remove(d); d.DataArrived(ret); break; } } mutex.ReleaseMutex(); }
/// <summary> /// 一个单独的线程用于不停地遍历,接收信号 /// </summary> void WaitThread() { DateTime lasttime = DateTime.Now; while (true) { State = QueueState.Waiting; int index = ManualResetEvent.WaitAny(evts, Interval, false); MessageArrivedEvent tmp = MessageArrived; State = QueueState.Running; bool senttick = false; for (int i = 0; i < Queues.Count; i++) { CurrentQueue = i; if (PriorityOfTick == i) { senttick = true; SendTickEvent(ref lasttime); } OneQueue q = Queues[i]; while (true) { OneQueue.Message m = q.PopData(); if (m == null) { break; } object ret = null; if (tmp != null) { bool req = m.type == MsgQueue.msgtype_Request; ret = tmp(this, m.sender, m.Data, req); } m.sender.MessageFinished(m, ret); } } if (!senttick) { SendTickEvent(ref lasttime); } } }
public DataSent(OneQueue.Message _msg) { msg = _msg; evt = new AutoResetEvent(false); }