项目需要刚写的Timer
Code
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Timers;
5
6
7namespace Server
8{
9 public class Stimer
10 {
11 private int times, _timers;
12 private Timer t;
13 /**//// <summary>
14 /// Elapsed事件的频率 单位:秒
15 /// </summary>
16 public int Interval
17 {
18 set { this.times = value; }
19 }
20 public Stimer(int _t)
21 {
22 t = new Timer(1000);//配置间隔时间为1000毫秒;
23 t.Elapsed += new ElapsedEventHandler(t_Elapsed);
24 t.AutoReset = true; ;//配置是执行一次(false)还是一直执行(true);
25 t.Enabled = false;//是否执行System.Timers.Timer.Elapsed事件;
26 this.times = _t;
27 this._timers = _t;
28 }
29 public void Start()
30 {
31 t.Start();
32 }
33 public void Stop()
34 {
35 t.Stop();
36 }
37 public void Restart()
38 {
39 t.Stop();
40 this._timers = times;
41 t.Start();
42 }
43 public void Dispose()
44 {
45 t.Stop();
46 t.Dispose();
47 }
48
49 public void TimeOut()
50 {
51 Console.WriteLine("aaaa");
52 }
53 void t_Elapsed(object sender, ElapsedEventArgs e)
54 {
55 if (_timers == 0)
56 {
57 _timers = times;
58 TimeOut();
59 }
60 else
61 {
62 _timers--;
63 }
64 }
65 }
66}
67
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Timers;
5
6
7namespace Server
8{
9 public class Stimer
10 {
11 private int times, _timers;
12 private Timer t;
13 /**//// <summary>
14 /// Elapsed事件的频率 单位:秒
15 /// </summary>
16 public int Interval
17 {
18 set { this.times = value; }
19 }
20 public Stimer(int _t)
21 {
22 t = new Timer(1000);//配置间隔时间为1000毫秒;
23 t.Elapsed += new ElapsedEventHandler(t_Elapsed);
24 t.AutoReset = true; ;//配置是执行一次(false)还是一直执行(true);
25 t.Enabled = false;//是否执行System.Timers.Timer.Elapsed事件;
26 this.times = _t;
27 this._timers = _t;
28 }
29 public void Start()
30 {
31 t.Start();
32 }
33 public void Stop()
34 {
35 t.Stop();
36 }
37 public void Restart()
38 {
39 t.Stop();
40 this._timers = times;
41 t.Start();
42 }
43 public void Dispose()
44 {
45 t.Stop();
46 t.Dispose();
47 }
48
49 public void TimeOut()
50 {
51 Console.WriteLine("aaaa");
52 }
53 void t_Elapsed(object sender, ElapsedEventArgs e)
54 {
55 if (_timers == 0)
56 {
57 _timers = times;
58 TimeOut();
59 }
60 else
61 {
62 _timers--;
63 }
64 }
65 }
66}
67