[求助]关于一个system.timers的问题
有于心急,所以把这篇文章弄到首页,如果写的比较垃圾,请大家原谅
最近要写一个像新浪乐居的程序,具体的是让每一个用户的体力按时间以不同的速度增加,我建了一个实体类代码如下:
Code
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)
{ }
}
}
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)
{ }
}
}
也就是每个用户都给实例一个体力信息,里面封装一个计数器,然后按时间增加他的体力,对这个信息的打操作类代码如下:
Code
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace PowerManage
6{
7
8 public class PowerManage : IPowerProvider
9 {
10 public Dictionary<int, PowerEntity> userdic = new Dictionary<int, PowerEntity>();
11 private static object state = new object();
12 /**//// <summary>
13 ///初始化单个用户体力计数器
14 /// </summary>
15 /// <param name="uid"></param>
16 public void Install(int uid)
17 {
18 if (!userdic.ContainsKey(uid))
19 {
20 lock (state)
21 {
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)
33 {
34 lock (state)
35 {
36 if (userdic.ContainsKey(uid))
37 {
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
45 {
46 Install(uid);
47 }
48 }
49 }
50 public void CutInterval(int num, int uid)
51 {
52
53 lock (state)
54 {
55 if (userdic.ContainsKey(uid))
56 {
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
64 {
65 Install(uid);
66 }
67 }
68
69 }
70 public void ClearPower(int uid)
71 {
72 lock (state)
73 {
74 if (userdic.ContainsKey(uid))
75 {
76 PowerEntity power = userdic[uid];
77 power.PowerValue = 0;
78 }
79 else
80 {
81 Install(uid);
82 }
83 }
84 }
85 public int GetPower(int uid)
86 {
87 lock (state)
88 {
89 if (userdic.ContainsKey(uid))
90 {
91 return userdic[uid].PowerValue;
92 }
93 else
94 {
95 Install(uid);
96 return 0;
97 }
98 }
99 }
100 }
101}
102
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace PowerManage
6{
7
8 public class PowerManage : IPowerProvider
9 {
10 public Dictionary<int, PowerEntity> userdic = new Dictionary<int, PowerEntity>();
11 private static object state = new object();
12 /**//// <summary>
13 ///初始化单个用户体力计数器
14 /// </summary>
15 /// <param name="uid"></param>
16 public void Install(int uid)
17 {
18 if (!userdic.ContainsKey(uid))
19 {
20 lock (state)
21 {
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)
33 {
34 lock (state)
35 {
36 if (userdic.ContainsKey(uid))
37 {
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
45 {
46 Install(uid);
47 }
48 }
49 }
50 public void CutInterval(int num, int uid)
51 {
52
53 lock (state)
54 {
55 if (userdic.ContainsKey(uid))
56 {
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
64 {
65 Install(uid);
66 }
67 }
68
69 }
70 public void ClearPower(int uid)
71 {
72 lock (state)
73 {
74 if (userdic.ContainsKey(uid))
75 {
76 PowerEntity power = userdic[uid];
77 power.PowerValue = 0;
78 }
79 else
80 {
81 Install(uid);
82 }
83 }
84 }
85 public int GetPower(int uid)
86 {
87 lock (state)
88 {
89 if (userdic.ContainsKey(uid))
90 {
91 return userdic[uid].PowerValue;
92 }
93 else
94 {
95 Install(uid);
96 return 0;
97 }
98 }
99 }
100 }
101}
102
接口:
Code
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace PowerManage
6{
7 public interface IPowerProvider
8 {
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
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace PowerManage
6{
7 public interface IPowerProvider
8 {
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里面这样写:
Code
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();
}
}
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,好像那个计数器不起作用啊,请那位大哥帮忙解释下