C # Multithreading-Marshaling call methods via event delegates between different threads

Source: Internet
Author: User
Tags contains count event timer
Multithreading

A custom single timer was made two days ago to make the appropriate event call based on the record ID and the set minutes minutes in the corresponding data record (ROW), but it cannot be invoked correctly if the event handler is in a form. But it's OK to take 82 to 93 notes out.

The timer is broadly 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 be timed to different timing events according to the same timer timing benchmark.
8 *
9 * Mytimer.timer contains a hashtable and Threading.timer, each timer callback
10 * Iterate over the Hashtable and determine whether to call the Timernode based on the timing period value of the zero
11 * The corresponding Timercome event.
12 ************************************************************ * *
Namespace MyTimer
14 {
///<summary>
16///Event Timer node
///</summary>
Internal class Timernode
19 {
///<summary>
21///Constructors
///</summary>
///<param name= "Timecount" > Periodic number </param>
///<param name= "EvtID" > Event id</param>
Timernode Public (long Timecount,object EvtID)
26 {
This.mtimecount=timecount;
This.mevtid=evtid;
29}
Private long Mtimecount;
to private object Mevtid;
32
Timecount Long
34 {
Get{return Mtimecount}
Set{mtimecount=value;}
37}
EvtID public Object
39 {
Get{return Mevtid;}
41}
42}
43
The public class Timereventargs:eventargs
45 {
The private System.Collections.ArrayList mevtids;
The public System.Collections.ArrayList Evtids
48 {
Get{return Mevtids;
50}
51
///<summary>
53///Construction
///</summary>
///<param name= "evtids" > Triggered Event IDs list </param>
Timereventargs Public (System.Collections.ArrayList evtids): Base ()
57 {
This.mevtids=evtids;
59}
60}
61
The public delegate void Timereventhandler (Timereventargs e);
63
///<summary>
///Timer Single-piece mode, cannot be instantiated.
///</summary>
public class Timer
68 {
///<summary>
70///node timed to event
///</summary>
The public static event Timereventhandler Timecome;
73
///<summary>
75///Wake up Timecome event.
///</summary>
///<param name= "E" > This parameter contains timed to event list </param>
The static void Raisetimecome (Timereventargs e)
79 {
if (timecome!=null)
81 {
I//if (Timecome.target is System.ComponentModel.ISynchronizeInvoke)
83//{
System.ComponentModel.ISynchronizeInvoke//Asynch=timecome.target as System.ComponentModel.ISynchro Nizeinvoke;
I//if (asynch.invokerequired)
86//{
A//object[] args=new object[1]{e};
A//Asynch.begininvoke (Timecome,args);
89//}
/Else
/Timecome (e);
92//}
I/or else
Timecome (e);
95}
96}
The ReadOnly long mperiod=1000*60;//interval is timed to 1 minutes.
Mtimer static System.Threading.Timer;
Static Timer ()
100 {
Mtimer=new System.Threading.Timer (New TimerCallback (timearrive), null,timeout.infinite,mperiod);
102}
103
///<summary>
105///Timer starts running
///</summary>
The public static void Run ()
108 {
109 Mtimer.change (0,mperiod);
110}
111
112///<summary>
113///Timer Stop.
114///</summary>
Up to public static void Stop ()
116 {
117 Mtimer.change (Timeout.infinite,mperiod);
118}
119
///<summary>
121///Adds a timed event and modifies its timing cycle if the timed event already exists.
122///</summary>
123///<param name= "EvtID" > Event id</param>
124///<param name= "Timecount" > Cycle number </param>
The 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///<summary>
136///Remove this timed event
137///</summary>
138///<param name= "EvtID" > Event id</param>
139 public static void Remove (object EvtID)
140 {
if (Mtimernodes.containskey (EvtID))
Mtimernodes.remove (EvtID);
143}
144
145///<summary>
146///This function is the callback function of the reference timer Mtimer,
147///In this function, the event table is checked, and the number of scheduled event cycles has been added to the event parameters
148///and wakes up the event.
149///</summary>
///<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}
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}
180



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.