自制定长的Queue

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

 

ContractedBlock.gif ExpandedBlockStart.gif 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;
            }
        }
    }
}
posted on 2008-08-04 11:30  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2008/08/04/1259769.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值