Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)delv
published in(发表于) 2014/1/8 7:02:39
C#多线程:不同线程之间通过事件委托封送调用方法_[Asp.Net教程]

C#多线程-不同线程之间通过事件委托封送调用方法_[Asp.Net教程]

前两天做了一个自定义单件Timer,该Timer能够根据相应数据记录(Row)中的记录ID和设定分钟Minutes 做相应的事件调用,但是如果此事件处理程序在一Form中时则不能正确调用它,但是把82到93行的注释去掉就可以了。

Timer大体定义如下:


1 using System;
2 using System.Threading;
3 using System.ComponentModel;
4 using System.Windows.Forms;
5
6 /************************************************************
7 * MyTimer.Timer能够根据同一Timer定时基准对不同的定时事件做定时。
8 *
9 * MyTimer.Timer包含一Hashtable和Threading.Timer,每次Timer定时回调
10 * 遍历Hashtable并根据其中的TimerNode的定时周期值是否为零来判断是否调用
11 * 相应的TimerCome事件。
12 ************************************************************ */
13 namespace MyTimer
14 {
15 ///


16 /// 事件定时节点
17 ///

18 internal class TimerNode
19 {
20 ///
21 /// 构造函数
22 ///

23 /// 定时周期数
24 /// 事件ID
25 public TimerNode(long TimeCount,object EvtID)
26 {
27 this.mTimeCount=TimeCount;
28 this.mEvtID=EvtID;
29 }
30 private long mTimeCount;
31 private object mEvtID;
32
33 public long TimeCount
34 {
35 get{return mTimeCount;}
36 set{mTimeCount=value;}
37 }
38 public object EvtID
39 {
40 get{return mEvtID;}
41 }
42 }
43
44 public class TimerEventArgs:EventArgs
45 {
46 private System.Collections.ArrayList mEvtIDs;
47 public System.Collections.ArrayList EvtIDs
48 {
49 get{return mEvtIDs;}
50 }
51
52 ///
53 /// 构造
54 ///

55 /// 触发的事件ID列表
56 public TimerEventArgs(System.Collections.ArrayList EvtIDs):base()
57 {
58 this.mEvtIDs=EvtIDs;
59 }
60 }
61
62 public delegate void TimerEventHandler(TimerEventArgs e);
63
64 ///
65 /// Timer 单件模式,不能实例化。
66 ///

67 public class Timer
68 {
69 ///
70 /// 有节点定时到事件
71 ///

72 public static event TimerEventHandler TimeCome;
73
74 ///
75 /// 唤醒TimeCome事件。
76 ///

77 /// 此参数包含定时到事件列表
78 static void RaiseTimeCome(TimerEventArgs e)
79 {
80 if(TimeCome!=null)
81 {
82 // if(TimeCome.Target is System.ComponentModel.ISynchronizeInvoke)
83 // {
84 // System.ComponentModel.ISynchronizeInvoke aSynch=TimeCome.Target as System.ComponentModel.ISynchronizeInvoke;
85 // if(aSynch.InvokeRequired)
86 // {
87 // object[] args=new object[1]{e};
88 // aSynch.BeginInvoke(TimeCome,args);
89 // }
90 // else
91 // TimeCome(e);
92 // }
93 // else
94 TimeCome(e);
95 }
96 }
97 static readonly long mPeriod=1000*60;//定时间隔1分钟。
98 static System.Threading.Timer mTimer;
99 static Timer()
100 {
101 mTimer=new System.Threading.Timer(new TimerCallback(TimeArrive),null,Timeout.Infinite,mPeriod);
102 }
103
104 ///
105 /// 定时器开始运行
106 ///

107 public static void Run()
108 {
109 mTimer.Change(0,mPeriod);
110 }
111
112 ///
113 /// 定时器停止。
114 ///

115 public static void Stop()
116 {
117 mTimer.Change(Timeout.Infinite,mPeriod);
118 }
119
120 ///
121 /// 加入定时事件,如果此定时事件已存在则修改其定时周期。
122 ///

123 /// 事件ID
124 /// 周期数
125 public static void Add(object EvtID,long TimeCount)
126 {
127 if(mTimerNodes.ContainsKey(EvtID))
128 {
129 ((TimerNode)mTimerNodes[EvtID]).TimeCount=TimeCount;
130 }
131 else
132 mTimerNodes.Add(EvtID,new TimerNode(TimeCount,EvtID));
133 }
134
135 ///
136 /// 移除此定时事件
137 ///

138 /// 事件ID
139 public static void Remove(object EvtID)
140 {
141 if(mTimerNodes.ContainsKey(EvtID))
142 mTimerNodes.Remove(EvtID);
143 }
144
145 ///
146 /// 此函数是基准定时器mTimer的回调函数,
147 /// 在此函数中将检查事件表,如期事件定时周期数已到则将其加入事件参数中
148 /// 并唤醒事件。
149 ///

150 ///
151 static void TimeArrive(object state)
152 {
153 System.Collections.ArrayList EvtIDs=new System.Collections.ArrayList();
154 foreach(TimerNode aNode in mTimerNodes.Values)
155 {
156 aNode.TimeCount--;
157 if(aNode.TimeCount<=0)
158 {
159 EvtIDs.Add(aNode.EvtID);
160 }
161 }
162 if(EvtIDs.Count>0)
163 {
164 for(int i=0;i165 {
166 mTimerNodes.Remove(EvtIDs[i]);
167 }
168 RaiseTimeCome(new TimerEventArgs(EvtIDs));
169 }
170 }
171
172 ///
173 /// 事件表
174 ///

175 static System.Collections.Hashtable mTimerNodes=new System.Collections.Hashtable();
176 }
177
178
179 }
180


来源:网络







If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759