C# 事件基礎知識

來源:互聯網
上載者:User

標籤:註冊   ace   驅動程式   自訂   lin   訊號   collect   .net   步驟   

事件是什麼

       一個事件代表程式中發生一件顯著事情的訊號。它包括事件的發送方和接收方。我們把觸發事件的對象稱為事件的寄件者,響應事件的對象稱為事件的接收者。在C#中,事件和委託經常聯絡在一塊,他們的關係就是對象和類的關係。

       在C#的表單應用開發中,.NET 中預定義了許多種專門用於事件的委託類型,比如 EventHandler、 KeyEventHandler、 MouseEventHandler 等。因此這裡主要介紹怎麼自訂一個事件。

怎麼使用事件

 (1)使用事件其實就是一個建立事件驅動程式的過程。一般採取的步驟如下:

     1.聲明關於事件的委託;
     2.聲明事件;
     3.編寫觸發事件的函數;
     4.建立事件處理常式;
     5.註冊事件處理常式;
     6.在適當的條件下觸發事件。

    (2) 編寫一個自訂事件。

       深林裡有一隻兔子和一隻羊,晚上兔子睡覺的時候,羊負責看守洞口。如果有狼出沒,羊就發出一個 Alarm 事件,兔子接到 Alarm 事件後就和羊一起採取行動。假設狼於 2017 年8月3日晚上12點整到達。

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace eventDemo 8 { 9     class Sheep10     { 11         //1.聲明關於事件的委託;12         public delegate void AlarmEventHandler(object sender, EventArgs e);13         14         //2.使用event關鍵字聲明事件;15         public event AlarmEventHandler Alarm;  16         17         public void OnAlarm()      //3.編寫引發事件的函數;18         {19             if (this.Alarm != null)20                 this.Alarm(this, new EventArgs()); //發出警報21         }22     }23         //事件接收者24     class Rabbit25     {26         //4.編寫事件處理常式27         void RabbitHandleAlarm(object sender, EventArgs e)28         {29             Console.WriteLine("兔子: 狼來啦,快躲起來! ");30         }31         //5.註冊事件處理常式;32         public Rabbit(Sheep sheep)33         {34             sheep.Alarm += new Sheep.AlarmEventHandler(RabbitHandleAlarm);35         }36     }37     //6.現在來觸發事件38     class Program39     {40         static void Main(string[] args)41         {42             Sheep sheep = new Sheep();43             Rabbit rabbit = new Rabbit(sheep);44             DateTime now = new DateTime(2017, 8, 2, 23, 59, 55);45             DateTime midnight = new DateTime(2017, 8, 3, 0, 0, 0);46             Console.WriteLine("時間一秒一秒地流逝,等待午夜的到來... ");47             while (true)48             {49                 Console.WriteLine("目前時間: " + now);50                 //午夜零點狼出沒,羊引發 Alarm 事件51                 if (now == midnight)52                 {53                     Console.WriteLine("\n 沙沙沙, 狼悄悄地來到了羊的附近...");54                     //引發事件55                     Console.WriteLine("\n 羊警示: 有狼出沒,咩~~~~~~~");56                     sheep.OnAlarm();57                     break;58                 }59                 System.Threading.Thread.Sleep(1000); //程式暫停一秒60                 now = now.AddSeconds(1); //時間增加一秒61             }62         }63     }64 }

         當午夜時分小偷到達時, sheep 調用 sheep.OnAlarm()函數,從而觸發 Alarm 事件,於是系統找到並執行了註冊在 Alarm 事件中的事件處理常式 RabbitHandleAlarm()。引發事件的代碼
(即 OnAlarm()函數)和事件處理常式是分離的,引發事件的代碼只管調用“Alarm()”,而事件處理常式另外獨立定義,它們之間通過事件 Alarm 聯絡起來。 

注意事項

 事件處理常式的參數應該和事件委託相同。一般情況下,事件處理常式接受兩個參數,

一個是事件的寄件者sender,一個是事件參數e 。

Ending.....

C# 事件基礎知識

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.