/// <summary> /// 此方法压入元素(message)并循环递增生产指针,当遇到报告指针时hold住直到报告指针被往前移动。 /// </summary> /// <param name="message">需要压入队列的message</param> /// <returns>put进去的元素在队列中的位置;如果由于异常而调用失败,函数返回-1</returns> public int put(Message message) { Monitor.Enter(thisLock); try { while (unreportedCount == size) { //Console.WriteLine("unreportedCount == size waiting report"); Monitor.Wait(thisLock); } //放入消息的时候带上自己的位置 message.index = producerP; messageContent[producerP] = message; int tempP = producerP; if (++producerP == size) producerP = 0; ++unreportedCount; ++count; Monitor.Pulse(thisLock); return tempP; } catch(ThreadInterruptedException e) { Console.WriteLine(e.StackTrace); return -1; } finally { Monitor.Exit(thisLock); } }
public Boolean handle(Message message) { try { topCometMessageListener.OnReceiveMsg(message.message); } catch (Exception) { //只要有异常就算失败 return false; } return true; }