ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 委托实现动态时间刷新

委托实现动态时间刷新

原创 Linux操作系统 作者:iDotNetSpace 时间:2008-09-09 13:14:32 0 删除 编辑
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(this, new EventArgs());

        }

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

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->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(this, new EventArgs());

        }

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

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-442647/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: Sqlite使用说明
请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5766214