程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> Events基本概念----Beginning Visual C#,

Events基本概念----Beginning Visual C#,

編輯:C#入門知識

Events基本概念----Beginning Visual C#,


一個事件的發生需要有觸發條件,如定時發生,鼠標點擊等,以及即將發生的事件本身,因此,events相關的最簡單的示例如下:

// 時間觸發器
class TimerTrigger
{
    private Timer myTimer;

    public TimerTrigger() 
    {
        myTimer = new Timer(100);
    }
    
    public TimerTrigger(int timeInterval)
    {
        myTimer = new Timer(timeInterval);
    }

    // 向時間觸發器添加具事件的handler
    public void AddEventHander(ElapsedEventHandler source)
    {
        myTimer.Elapsed += source;
    }

    public void Start()
    {
        myTimer.Start();
    }

    public void Stop()
    {
        myTimer.Stop();
    }
}

class Program
{
    static int counter = 0;
    static string displayString = 
        "This string will appear one letter at a time. ";

    // 即將被添加到時間觸發器的事件
    static void WriteChar(object source, ElapsedEventArgs e)
    {
        Console.Write(displayString[counter++ % displayString.Length]);
    }

    public static void Main()
    {
        TimerTrigger timerTrigger = new TimerTrigger(100);
        timerTrigger.AddEventHander(new ElapsedEventHandler(WriteChar));
        timerTrigger.Start();
        Console.ReadLine();
    }
}

在上面的例子的main函數中,直接使用了c#自帶的ElapsedEventHandler delegated類型,那麼如何自定義事件處理函數呢?具體如下:

// 自定義時間處理代表
public delegate void MessageHandler(string message);

class TimerTrigger
{
    private Timer myTimer;
    private event MessageHandler MessageArrived;

    public TimerTrigger(int timeInterval)
    {
        myTimer = new Timer(timeInterval);
        myTimer.Elapsed += new ElapsedEventHandler(CheckForMessage);
    }

    public void AddEventHandler(MessageHandler source)
    {
        MessageArrived += source; 
    }

    public void Start()
    {
        myTimer.Start();
    }

    public void Stop()
    {
        myTimer.Stop();
    }

    private void CheckForMessage(object source, ElapsedEventArgs e)
    {
        Console.WriteLine("Checking for new messages.");
        if (MessageArrived != null)
        {
            MessageArrived("hello world");
        }

    }
}

class Program
{
    static int counter = 0;

    static void WriteFirstMessage(string message)
    {
        Console.WriteLine("The First {0} number: {1}", message, counter++);
    }

    static void WriteSecondMessage(string message)
    {
        Console.WriteLine("The Second {0} number: {1}", message, counter++);
    }

    public static void Main()
    {
        TimerTrigger timerTrigger = new TimerTrigger(100);
        timerTrigger.AddEventHandler(new MessageHandler(WriteFirstMessage));
        timerTrigger.AddEventHandler(new MessageHandler(WriteSecondMessage));
        timerTrigger.Start();
        Console.ReadLine();
    }
}

在上述的例子中,將消息傳遞給處理事件所用的函數為WriteFirstMessage(string message),其傳遞的參數為string類型的。再考慮道<Timer>.ElapsedEventHandler所代表的函數的傳遞參數為:(object source, ElapsedEventArgs e)兩個參數,那麼我們能否實現類似的形式呢?這是可以的,如下:

public class MessageArrivedEventArgs : EventArgs
{
    private string message;
    public string Message
    {
        get
        {
            return message;
        }
    }
    public MessageArrivedEventArgs()
    {
        message = "No message sent.";
    }
    public MessageArrivedEventArgs(string newMessage)
    {
        message = newMessage;
    }
}
         
public delegate void MessageHandler(TimerTrigger source,
    MessageArrivedEventArgs e);

public class TimerTrigger
{
    private Timer myTimer;
    private event MessageHandler MessageArrived;

    public TimerTrigger(int timeInterval)
    {
        myTimer = new Timer(timeInterval);
        myTimer.Elapsed += new ElapsedEventHandler(CheckForMessage);
    }

    public void AddEventHandler(MessageHandler source)
    {
        MessageArrived += source; 
    }

    public void Start()
    {
        myTimer.Start();
    }

    public void Stop()
    {
        myTimer.Stop();
    }

    private void CheckForMessage(object source, ElapsedEventArgs e)
    {
        Console.WriteLine("Checking for new messages.");
        if (MessageArrived != null)
        {
            MessageArrived(this, new MessageArrivedEventArgs("hello world"));
        }

    }
}

class Program
{
    static int counter = 0;

    static void WriteFirstMessage(TimerTrigger source, MessageArrivedEventArgs e)
    {
        Console.WriteLine("The First {0} number: {1}", e.Message, counter++);
    }

    static void WriteSecondMessage(TimerTrigger source, MessageArrivedEventArgs e)
    {
        Console.WriteLine("The Second {0} number: {1}", e.Message, counter++);
    }

    public static void Main()
    {
        TimerTrigger timerTrigger = new TimerTrigger(100);
        timerTrigger.AddEventHandler(new MessageHandler(WriteFirstMessage));
        timerTrigger.AddEventHandler(new MessageHandler(WriteSecondMessage));
        timerTrigger.Start();
        Console.ReadLine();
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved