我希望你是我独家记忆

一段永远封存的记忆,随风而去
posts - 263, comments - 31, trackbacks - 0, articles - 3
   :: 首页 :: 新随笔 ::  :: 聚合  :: 管理

C#事件

Posted on 2009-06-23 13:49 Hero 阅读(143) 评论(0)  编辑 收藏 引用 所属分类: C#积累
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 using System.Threading;
 5 
 6 namespace MyEvent
 7 {
 8     public class MyTimerClass
 9     {
10         public event EventHandler Elapsed;//声明事件
11 
12         private void OnOneSecond( object source, EventArgs args )
13         {
14             if ( Elapsed != null ) Elapsed( source, args );
15         }
16 
17         private System.Timers.Timer MyPrivateTimer;//设置私有计时器
18         public MyTimerClass()
19         {
20             MyPrivateTimer = new System.Timers.Timer();//创建私有计时器
21 
22             //添加事件处理程序
23             MyPrivateTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnOneSecond);
24 
25             MyPrivateTimer.Interval = 1000;
26             MyPrivateTimer.Enabled = true;
27         }
28     }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
29 
30     class ClassA
31     {
32         public void TimerHandlerA( object obj, EventArgs e )
33         {//事件处理程序
34             Console.WriteLine( "Class A handler called " );
35         }
36     }
37 
38     class ClassB
39     {
40         public static void TimerHandlerB( object obj, EventArgs e )
41         {
42             Console.WriteLine( "Class B handler called " );
43         }
44     }
45 
46     class Program
47     {
48         static void Main( string[] args )
49         {
50             MyTimerClass mtc = new MyTimerClass();
51             ClassA ca = new ClassA();
52 
53             //mtc.Elapsed += ca.TimerHandlerA;     //添加事件A处理程序
54             //mtc.Elapsed += ClassB.TimerHandlerB; //添加处理程序B
55 
56             mtc.Elapsed += new EventHandler( ca.TimerHandlerA );
57             mtc.Elapsed += new EventHandler( ClassB.TimerHandlerB );
58 
59             Thread.Sleep( 2250 );
60         }
61     }
62 }
63 

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理