[求助]关于一个system.timers的问题

本文介绍了一个用于模拟用户体力随时间增长的程序设计案例。通过创建实体类和管理类实现每位用户的体力按不同时间间隔自动增加的功能。然而,在实际运行过程中遇到了计数器未正常工作的问题。

有于心急,所以把这篇文章弄到首页,如果写的比较垃圾,请大家原谅

 

最近要写一个像新浪乐居的程序,具体的是让每一个用户的体力按时间以不同的速度增加,我建了一个实体类代码如下:

 

ContractedBlock.gifExpandedBlockStart.gifCode
using System;
using System.Collections.Generic;
using System.Text;
using Discuz.Data;
namespace PowerManage
{
    
public class PowerEntity
    {
        
private int _Userid;
        
private int _interval;
        
private int _powervalue;
       
        
public int UserId
        {
            
get { return _Userid; }
            
set { _Userid = value; }
        }
        
public int Interval
        {
            
get { return _interval; }
            
set { _interval = value; }
        }
        
public int PowerValue
        {
            
get { return _powervalue; }
            
set { _powervalue = value; }
        }
       
        
public  System.Timers.Timer time=new System.Timers.Timer();
       
        
public void PowerUp()
        {
            
lock (this)
            {
                time.Interval 
= this._interval;
                time.Enabled 
= true;
                time.AutoReset 
= true;
                time.Elapsed 
+= new System.Timers.ElapsedEventHandler(UpdatePower);
                time.Start();
            }

        }
       
        
private void UpdatePower(object sender, System.Timers.ElapsedEventArgs e)
        {
            
            
this._powervalue=this._powervalue+1;
        }
        
private void UpdataData(object sender, System.Timers.ElapsedEventArgs e)
        { }

    }

}

也就是每个用户都给实例一个体力信息,里面封装一个计数器,然后按时间增加他的体力,对这个信息的打操作类代码如下:

 

ContractedBlock.gifExpandedBlockStart.gifCode
  1using System;
  2using System.Collections.Generic;
  3using System.Text;
  4
  5namespace PowerManage
  6ExpandedBlockStart.gifContractedBlock.gif{
  7
  8    public class PowerManage : IPowerProvider
  9ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 10        public Dictionary<int, PowerEntity> userdic = new Dictionary<int, PowerEntity>();
 11        private static object state = new object();
 12ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 13        ///初始化单个用户体力计数器
 14        /// </summary>
 15        /// <param name="uid"></param>

 16        public void  Install(int uid)
 17ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 18            if (!userdic.ContainsKey(uid))
 19ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 20                lock (state)
 21ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 22                    PowerEntity powerinfo = new PowerEntity();
 23                    powerinfo.UserId = uid;
 24                    powerinfo.Interval = 1000;
 25                    powerinfo.PowerValue = 0;
 26                    powerinfo.PowerUp();
 27                    userdic.Add(uid, powerinfo);
 28                    
 29                }

 30            }

 31        }

 32        public void ChangeInterval(int num,int uid)
 33ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 34            lock (state)
 35ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 36                if (userdic.ContainsKey(uid))
 37ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 38                    PowerEntity power = userdic[uid];
 39                    power.time.Enabled = false ;
 40                    power.Interval = power.Interval + num;
 41                    power.time.Interval = power.Interval;
 42                    power.time.Enabled = true;
 43                }

 44                else
 45ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 46                    Install(uid);
 47                }

 48            }

 49        }

 50        public void CutInterval(int num, int uid)
 51ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 52
 53            lock (state)
 54ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 55                if (userdic.ContainsKey(uid))
 56ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 57                    PowerEntity power = userdic[uid];
 58                    power.time.Enabled = false;
 59                    power.Interval = power.Interval - num;
 60                    power.time.Interval = power.Interval;
 61                    power.time.Enabled = true;
 62                }

 63                else
 64ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 65                    Install(uid);
 66                }

 67            }

 68
 69        }

 70        public void ClearPower(int uid)
 71ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 72            lock (state)
 73ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 74                if (userdic.ContainsKey(uid))
 75ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 76                    PowerEntity power = userdic[uid];
 77                    power.PowerValue = 0;
 78                }

 79                else
 80ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 81                    Install(uid);
 82                }

 83            }

 84        }

 85        public int GetPower(int uid)
 86ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 87            lock (state)
 88ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 89                if (userdic.ContainsKey(uid))
 90ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 91                    return userdic[uid].PowerValue;
 92                }

 93                else
 94ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 95                    Install(uid);
 96                    return 0;
 97                }

 98            }

 99        }

100    }

101}

102

 

接口:

 

ContractedBlock.gifExpandedBlockStart.gifCode
 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4
 5namespace PowerManage
 6ExpandedBlockStart.gifContractedBlock.gif{
 7   public  interface  IPowerProvider
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 9       
10        void  Install(int uid);
11        void ChangeInterval(int num, int uid);
12        void CutInterval(int num, int uid);
13        void ClearPower(int uid);
14    }

15}

16

 

不知道怎么回事我在demo.aspx里面这样写:

 

ContractedBlock.gifExpandedBlockStart.gifCode
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PowerManage;
using Discuz.Forum;
using Discuz.Common;
public partial class myspace_demo : System.Web.UI.Page
{
    PowerManage.PowerManage pm 
= new PowerManage.PowerManage();
    
int uid = 0;
    
protected void Page_Load(object sender, EventArgs e)
    {
        
if (!IsPostBack)
        {
            uid 
= Utils.StrToInt(ForumUtils.GetCookie("userid"), 0);
            
if (uid == 0)
            {
                Response.Redirect(
"/myspace/login.aspx");
                
return;
            }


            pm.Install(uid);
        }

        
        
    }
    
protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox1.Text 
= pm.GetPower(uid).ToString() + pm.userdic.ContainsKey(uid).ToString() + pm.userdic[uid].time.Interval.ToString();
    }
}

 

怎么得到体力一直是0,好像那个计数器不起作用啊,请那位大哥帮忙解释下

转载于:https://www.cnblogs.com/hl0071/archive/2009/05/31/1492757.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值