Visual Studio EventHandler Delegate 和 EventArgs

來源:互聯網
上載者:User

標籤:blog   http   os   io   資料   ar   cti   div   

EventHandler代理 用來表示處理一個沒有事件數目據(event data)的事件(event)的 方法。

無論何時事件發生時,事件代理就被調用來觸發以前事件驅動的其他事件(監聽當前事件TCurrentEvent += TListenerEvent)。

public delegate void EventHandler(Object sender,    //事件發起者EventArgs e       //處理事件數目據的對象)

 

EventArgs 類是事件變數的基類,提供事件數目據類型給某個事件。

System.Object 
  System.EventArgs
    More...

public class EventArgs  

 

The following example shows an event named ThresholdReached that is associated with an EventHandler delegate. The method assigned to the EventHandler delegate is called in the OnThresholdReached method.

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace delegrate{    class Program    {        static void Main(string[] args)        {            Counter c = new Counter(new Random().Next(10));            c.ThresholdReached += c_ThresholdReached;  //聲明一個事件監聽            Console.WriteLine("press ‘a‘ key to increase total");            while (Console.ReadKey(true).KeyChar == ‘a‘)            {                Console.WriteLine("adding one");                  c.Add(1);                }        }        static void c_ThresholdReached(object sender, ThresholdReachedEventArgs e)        {            Console.WriteLine("The threshold of {0} was reached at {1}.", e.Threshold, e.TimeReached);            Environment.Exit(0);        }    }    class Counter    {        private int threshold;        private int total;        public Counter(int passedThreshold)        {            threshold = passedThreshold;        }        public void Add(int x)        {            total += x;            if (total >= threshold)            {                ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();                args.Threshold = threshold;                args.TimeReached = DateTime.Now;                OnThresholdReached(args);          //傳輸一個 TEventArgs-->ThresholdReachedEventArgs            }        }        protected virtual void OnThresholdReached(ThresholdReachedEventArgs e)        {            EventHandler<ThresholdReachedEventArgs> handler = ThresholdReached;  //handle an Event            if (handler != null)            {                handler(this, e);   //觸發 c_ThresholdReached            }        }        public event EventHandler<ThresholdReachedEventArgs> ThresholdReached;    }           public class ThresholdReachedEventArgs : EventArgs    {        public int Threshold { get; set; }        public DateTime TimeReached { get; set; }    }}

  

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.