委托与事件

public abstract class AbstractThread
    {
        Thread thread 
= null;

        
public abstract void run();

        
public void start()
        {
            
if (thread == null)
                thread 
= new Thread(new ThreadStart(run));
            thread.Start();
        }
    }
public delegate void TimeEventHandler(object obj, TimeEventArgs args); 

    
public class EventThread : AbstractThread
    {
        
public EventThread()
        {
            IsAlive 
= true;
        }

        
public event TimeEventHandler TimeChanged;

        
public bool IsAlive { getset; }

        
public override void run()
        {
            DateTime initi 
= DateTime.Now;
            
int h1 = initi.Hour;
            
int m1 = initi.Minute;
            
int s1 = initi.Second;
            
while (IsAlive)
            {
                DateTime now 
= DateTime.Now;
                
int h2 = now.Hour;
                
int m2 = now.Minute;
                
int s2 = now.Second;
                
if (s2 != s1)
                {
                    h1 
= h2;
                    m1 
= m2;
                    s1 
= s2;

                    
//首先建立一个TimeEventArgs对象来保存相关参数,这里是时分秒。
                    TimeEventArgs args = new TimeEventArgs(h2, m2, s2);
                    TimeChanged(
this, args);
                }
                Thread.Sleep(
1000);
            }


        }
    }

    
public class TimeEventArgs : EventArgs
    {
        
private int hour;
        
private int minute;
        
private int second;
        
public TimeEventArgs(int hour, int minute, int second)
        {
            
this.hour = hour;
            
this.minute = minute;
            
this.second = second;
        }
        
public int Hour
        {
            
get
            {
                
return hour;
            }
        }
        
public int Minute
        {
            
get
            {
                
return minute;
            }
        }
        
public int Second
        {
            
get
            {
                
return second;
            }
        }
    }

转载于:https://www.cnblogs.com/kenter/archive/2011/04/26/2029868.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值