C # multithreading-different threads use event Delegate to mail the call Method

Source: Internet
Author: User
A custom single-piece Timer was created two days ago. The Timer can call corresponding events based on the Record ID and Minutes in the corresponding data record (Row, however, if this event handler cannot call it correctly in a Form, you can remove the comments from lines 82 to 93.

Timer is defined as follows:

1 using System;
2 using System. Threading;
3 using System. ComponentModel;
4 using System. Windows. Forms;
5
6 /************************************** **********************
7 * MyTimer. Timer can schedule different scheduled events based on the same Timer timing benchmark.
8 *
9 * MyTimer. Timer contains a Hashtable and Threading. Timer, which calls back each time Timer.
10 * traverse Hashtable and determine whether to call it based on whether the scheduled cycle value of TimerNode is zero.
11 * corresponding TimerCome events.
12 *************************************** **********************/
13 namespace MyTimer
14 {
15 /// <summary>
16 // scheduled event Node
17 /// </summary>
18 internal class TimerNode
19 {
20 /// <summary>
21 // Constructor
22 /// </summary>
23 // <param name = "TimeCount"> Number of scheduled periods </param>
24 /// <param name = "EvtID"> event ID </param>
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 /// <summary>
53 // Structure
54 /// </summary>
55 // <param name = "EvtIDs"> List of event IDs triggered </param>
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 /// <summary>
65 // Timer single-piece mode, which cannot be instantiated.
66 /// </summary>
67 public class Timer
68 {
69 /// <summary>
70 // scheduled to event with Node
71 /// </summary>
72 public static event TimerEventHandler TimeCome;

 

/// <Summary>
75 // wake up the TimeCome event.
76 // </summary>
77 // <param name = "e"> this parameter contains the scheduled to event list </param>
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; // The scheduled interval is 1 minute.
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 /// <summary>
105 // The timer starts to run
106 /// </summary>
107 public static void Run ()
108 {
109 mTimer. Change (0, mPeriod );
110}
111
112 /// <summary>
113 // The timer stops.
114 /// </summary>
115 public static void Stop ()
116 {
117 mTimer. Change (Timeout. Infinite, mPeriod );
118}
119
120 /// <summary>
121 // Add a scheduled event. If the scheduled event already exists, modify its scheduled period.
122 /// </summary>
123 /// <param name = "EvtID"> event ID </param>
124 /// <param name = "TimeCount"> Number of cycles </param>
125 public static void Add (object EvtID, long TimeCount)
126 {
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 /// <summary>
136 // remove this scheduled event
137 /// </summary>
138 /// <param name = "EvtID"> event ID </param>
139 public static void Remove (object EvtID)
140 {
141 if (mTimerNodes. ContainsKey (EvtID ))
142 mTimerNodes. Remove (EvtID );
143}
144
145 /// <summary>
146 // This function is the callback function of the benchmark timer mTimer,
147 // The Event table will be checked in this function. If the scheduled number of scheduled events is reached, add it to the event parameters.
148 // and wake up the event.
149 /// </summary>
150 /// <param name = "state"> </param>
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; I <EvtIDs. Count; I ++)
165 {
166 mTimerNodes. Remove (EvtIDs [I]);
167}
168 RaiseTimeCome (new TimerEventArgs (EvtIDs ));
169}
170}
171
172 /// <summary>
173 // event table
174 /// </summary>
175 static System. Collections. Hashtable mTimerNodes = new System. Collections. Hashtable ();
176}
177
178
179}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.