好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

自制定长的Queue

自制定长的Queue

在我的程序中,需要实现这么一个功能,如果连续ping客户机三次都不成功,就表示该客户机下线了,每次ping间隔两秒钟,怎么来标记连续三次失败呢,于是就想到了Queue,可是Queue的长度是可以无限的,不太符合我的要求,那我就开始改造了,代码如下

Code
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;

namespace  Server
{
     public   class  ErrQueue:Queue < bool >
    {
         public  ErrQueue(): base ( 3 )
        {
             this .Add( true );
             this .Add( true );
             this .Add( true );
        }
         ///   <summary>
         ///  添加新元素,如果队列中固有元素已有3个,则弹出最先进去的一个
         ///  始终保持整个队列中至多有3个元素
         ///   </summary>
         ///   <param name="b"></param>
         public   void  Add( bool  b)
        {
             if  ( this .Count  >   3 )
                 this .Dequeue();
             this .Enqueue(b);
        }
         ///   <summary>
         ///  当元素小于3个或是3个元素中有一个或多个为true时返回false
         ///  仅当三个元素都为false时返回true
         ///   </summary>
         public   bool  IsErr
        {
             get
            {
                 if  ( this .Count  <   3 )
                     return   false ;
                 foreach  (var elem  in   this )
                     if  (elem)
                         return   false ;
                 return   true ;
            }
        }
    }
}

查看更多关于自制定长的Queue的详细内容...

  阅读:38次