ges/OutliningIndicators/None.gif[/img]
Ok,需要具备的元素我们都写好了,但是如何让它们工作起来?如果使Winform程序,那将毫无悬念。
分析:我们碰到的问题
第一个问题:当客户端发送一个数据包,我们是实例化一个新的Monitor么?如果是,哪么每次实例化一个全新的Monitor,所有在它上面订阅的事件将全部消失了,如果不是那这个Monitor将如何存在呢?总不能真空吧,两个http请求之间如何保存数据呢?不过再把需求一读,好像整个应用程序中就只需要也只能有一个这样的Monitor呢,该是单件模式上场的时候了。
在上面的Monitor的实现中添加下面的代码:
[img]http://www.cnblogs.com/Images/OutliningIndicators/None.gif[/img] private static Monitor _instance = null; [img]http://www.cnblogs.com/Images/OutliningIndicators/None.gif[/img]public static Monitor Current [img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif[/img] [img]http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif[/img] [img]http://www.cnblogs.com/Images/dot.gif[/img]{ [img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] get [img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img] [img]http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif[/img] [img]http://www.cnblogs.com/Images/dot.gif[/img]{ [img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] if (_instance == null) [img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] _instance = new Monitor(); [img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img] return _instance; [img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img] } [img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif[/img]}
但是本系统存在多个客户端,所以为了避免多线程造成问题,还是来Double Check一下吧,修改上面的代码如下:
[img]http://www.cnblogs.com/Images/OutliningIndicators/None.gif[/img] public static Monito |