国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

C#多線程-不同線程之間通過事件委托封送調用方法

2024-07-21 02:28:50
字體:
來源:轉載
供稿:網友

  前兩天做了一個自定義單件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     /// <summary>
 16     /// 事件定時節點
 17     /// </summary>
 18     internal class timernode
 19     {
 20         /// <summary>
 21         /// 構造函數
 22         /// </summary>
 23         /// <param name="timecount">定時周期數</param>
 24         /// <param name="evtid">事件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         /// 構造
 54         /// </summary>
 55         /// <param name="evtids">觸發的事件id列表</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 單件模式,不能實例化。
 66     /// </summary>
 67     public class timer
 68     {
 69         /// <summary>
 70         /// 有節點定時到事件
 71         /// </summary>
 72         public static event timereventhandler timecome;
 73
 74         /// <summary>
 75         /// 喚醒timecome事件。
 76         /// </summary>
 77         /// <param name="e">此參數包含定時到事件列表</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;//定時間隔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         /// <summary>
105         /// 定時器開始運行
106         /// </summary>
107         public static void run()
108         {
109             mtimer.change(0,mperiod);
110         }
111
112         /// <summary>
113         /// 定時器停止。
114         /// </summary>
115         public static void stop()
116         {
117             mtimer.change(timeout.infinite,mperiod);
118         }
119
120         /// <summary>
121         /// 加入定時事件,如果此定時事件已存在則修改其定時周期。
122         /// </summary>
123         /// <param name="evtid">事件id</param>
124         /// <param name="timecount">周期數</param>
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         /// <summary>
136         /// 移除此定時事件
137         /// </summary>
138         /// <param name="evtid">事件id</param>
139         public static void remove(object evtid)
140         {
141             if(mtimernodes.containskey(evtid))
142                 mtimernodes.remove(evtid);
143         }
144
145         /// <summary>
146         /// 此函數是基準定時器mtimer的回調函數,
147         /// 在此函數中將檢查事件表,如期事件定時周期數已到則將其加入事件參數中
148         /// 并喚醒事件。
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         /// 事件表
174         /// </summary>
175         static system.collections.hashtable mtimernodes=new system.collections.hashtable();
176     }
177
178
179 }
180

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 杭州市| 淅川县| 鹤岗市| 南开区| 怀来县| 衡阳市| 新宁县| 盐池县| 都安| 梨树县| 大埔区| 新乐市| 阿瓦提县| 靖安县| 无棣县| 修文县| 昌乐县| 武强县| 大新县| 宁津县| 秦安县| 桐乡市| 虎林市| 阳西县| 焉耆| 新宁县| 鹤岗市| 大同市| 北京市| 沙湾县| 延边| 内江市| 红河县| 衡山县| 张掖市| 宁河县| 社会| 日土县| 广南县| 长武县| 梁山县|