委托实现动态时间刷新

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace ConsoleApplication6
{
    
class Program
    {
        
static void Main(string[] args)
        {
            Demo obj 
= new Demo();
            obj.myEvent
+=new Demo.myDelegate(obj_myEvent);
            obj.Run();
            Console.ReadLine();
        }

        
private static void obj_myEvent(object sender, EventArgs e)
        {
            Console.Write(
"当前时间:" + DateTime.Now.ToLocalTime());
        }
    }

    
//事件处理程序委托的标准签名定义一个没有返回值的方法,其第一个参数的类型为 Object,它引用引发事件的实例,
    
//第二个参数从 EventArgs 类型派生,它保存事件数据。如果事件不生成事件数据,则第二个参数只是 EventArgs 的一个实例。否则,第二个参数为从 EventArgs 派生的自定义类型,
    
//提供保存事件数据所需的全部字段或属性。

    
class Demo
    {
        
public delegate void myDelegate(object sender, EventArgs e);//定义委托
        public event myDelegate myEvent;//定义委托类型的事件
        
//定义委托类型的方法
        public void Run()
        {
            
//定义一个每一秒钟触发的计时器
            System.Timers.Timer myTimer = new System.Timers.Timer(1000);
            
//定义时间间隔所要处理的方法, EventHandler 是一个预定义的委托,专用于表示不生成数据的事件的事件处理程序方法。如果事件生成数据,则必须提供自己的自定义事件数据类型,
            
//并且必须要么创建一个委托,其中第二个参数的类型为自定义类型,要么使用泛型 EventHandler 委托类并用自定义类型替代泛型类型参数。
            
            myTimer.Elapsed 
+= new System.Timers.ElapsedEventHandler(TimeEventHandler);
            myTimer.Interval 
= 1000;
            myTimer.Enabled 
= true;
            
//触发事件
            myEvent(thisnew EventArgs());

        }

        
//委托的方法
        public void TimeEventHandler(object sender, System.Timers.ElapsedEventArgs e)
        {
            Console.Clear();
            Console.Write(
"当前时间:"+ DateTime.Now.ToLocalTime());
        }
    }

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace ConsoleApplication6
{
    
class Program
    {
        
static void Main(string[] args)
        {
            Demo obj 
= new Demo();
            obj.myEvent
+=new Demo.myDelegate(obj_myEvent);
            obj.Run();
            Console.ReadLine();
        }

        
private static void obj_myEvent(object sender, EventArgs e)
        {
            Console.Write(
"当前时间:" + DateTime.Now.ToLocalTime());
        }
    }

    
//事件处理程序委托的标准签名定义一个没有返回值的方法,其第一个参数的类型为 Object,它引用引发事件的实例,
    
//第二个参数从 EventArgs 类型派生,它保存事件数据。如果事件不生成事件数据,则第二个参数只是 EventArgs 的一个实例。否则,第二个参数为从 EventArgs 派生的自定义类型,
    
//提供保存事件数据所需的全部字段或属性。

    
class Demo
    {
        
public delegate void myDelegate(object sender, EventArgs e);//定义委托
        public event myDelegate myEvent;//定义委托类型的事件
        
//定义委托类型的方法
        public void Run()
        {
            
//定义一个每一秒钟触发的计时器
            System.Timers.Timer myTimer = new System.Timers.Timer(1000);
            
//定义时间间隔所要处理的方法, EventHandler 是一个预定义的委托,专用于表示不生成数据的事件的事件处理程序方法。如果事件生成数据,则必须提供自己的自定义事件数据类型,
            
//并且必须要么创建一个委托,其中第二个参数的类型为自定义类型,要么使用泛型 EventHandler 委托类并用自定义类型替代泛型类型参数。
            
            myTimer.Elapsed 
+= new System.Timers.ElapsedEventHandler(TimeEventHandler);
            myTimer.Interval 
= 1000;
            myTimer.Enabled 
= true;
            
//触发事件
            myEvent(thisnew EventArgs());

        }

        
//委托的方法
        public void TimeEventHandler(object sender, System.Timers.ElapsedEventArgs e)
        {
            Console.Clear();
            Console.Write(
"当前时间:"+ DateTime.Now.ToLocalTime());
        }
    }
请使用浏览器的分享功能分享到微信等