Understanding for events and commission (3) Example

xiaoxiao2021-03-06  104

The examples described here are based on events and entrustments and incidents discussed in the event. This example shows how to raise events from your class and how to handle events. It defines the following classes. AlarmClock is a class that triggers an ALARM event. AlarMeventargs define data for ALARM events. AlarMeventHandler is a delegation of ALARM events. WakeMeup is a class with alarmrang method, which processes the ALARM event. AlarmDriver is a class that demonstrates how to connect. This class instantiates AlarmClock and WakeMeup. This class then instantiates AlarMeventHandler delegate by reference to the AlarmRANG method of the WakeMeup instance. AlarmDriver is entrustted to an event by registering the delegate and using = syntax to complete the event link. Compile and run this example copies the following code to the source file (such as eventsample.cs or eventsample.vb). Execute the following command from the directory containing the source file: [c #] csc /r :system.dll eventsample.cs

[Visual Basic] VBC /R :System.dll EventSample.vb (/ R option references the System assembly.) Run the created executable EventSample.exe.

[C #] // EventSample.cs.//namespace EventSample {using System; using System.ComponentModel; // Class that contains the data for // the alarm event Derives from System.EventArgs // public class AlarmEventArgs:. EventArgs {. private readonly bool snoozePressed; private readonly int nrings; // Constructor // public AlarmEventArgs (bool snoozePressed, int nrings) {this.snoozePressed = snoozePressed; this.nrings = nrings;}. // The NumRings property returns the number of rings / / that the alarm clock has sounded when the alarm event // is generated // public int NumRings. {get {return nrings;}} // The SnoozePressed property indicates // is pressed on the alarm when the alarm whether the snooze button event IS generated. // public bool snoozepressed {get {return snoozepressed;}} // the alarmte Xt Property That Contains The Wake-Up Message. // Public String AlarmText {Get {IF (Snoozepressed) {Return ("Wake Up !!! Snooze Time Is Over.");} else {return ("Wake Up!") ;}}}} // Delegate declaration // public delegate void AlarmEventHandler (object sender, AlarmEventArgs e);.. // The Alarm class that raises the alarm event // public class AlarmClock {private bool snoozePressed = false; private int nrings = 0; private bool stop = false;

// The Stop property indicates whether the // alarm should be turned off // public bool Stop {get {return stop;} set {stop = value;}}. // The SnoozePressed property indicates whether the snooze // button is pressed on the alarm when the alarm event is generated // public bool snoozePressed {get {return snoozePressed;} set {snoozePressed = value;}}. // The event member that is of type AlarmEventHandler // public event AlarmEventHandler Alarm.;

// The protected OnAlarm method raises the event by invoking // the delegates. The sender is always this, the current instance // of the class. // protected virtual void OnAlarm (AlarmEventArgs e) {if (Alarm! = Null) { // invokes the delegates. Alarm (this, e);}} // this alarm clock does not have simulated the alarm mechanism it has a loop // That Raises The Alarm Event At Every Ite // WITH A TIME DELAY OF 300 MILLISECONDS, // if Snooze is not press. if snooze ispered, // The time delay is 1000 milliseconds. // public void start () {for (;;) {nrings ; if {Break;} else if (snoozepressed) {system.threading.thread.sleep (1000); {alarmeventargs e = New AlarmEventArgs (snoozePressed, nrings); OnAlarm (e);}} else {System.Threading.Thread.Sleep (300); AlarmEventArgs e = new AlarmEventArgs (snoozePressed, nrings); OnAlarm (e);}}}} / / The WakeMeUp class that has a method AlarmRang that handles the // alarm event // public class WakeMeUp {public void AlarmRang (object sender, AlarmEventArgs e) {Console.WriteLine (e.AlarmText "/ n");. if ( (E.SNOZEPRESSED) {IF (e.numrings% 10 ==

0) {Console.WriteLine ("Let alarm Ring? Enter Y"); console.writeline ("Press Snooze? Enter N"; console.writeLine ("Stop Alarm? Enter Q"); string input = console.readline ); IF (INPUT.EQUALS ("Y")) Return; Else IF (INPUT.EQUALS ("N") || Input.Equals ("N")) {(((") { AlarmClock) Sender). Snoozepressed = true; return;} else {((alarmclock) sender .stop = true; return;}}} else {console.writeline ("Let alarm Ring? Enter Y"); console.writeline "STOP ALARM? ENTER Q"; string infut = console.readline (); if (INPUT.EQUALS ("Y") || input.equals ("y")) Return; Else {((alarmclock). STOP = True; Return;}}} /////in. // in A Forms-based Application, The Driver Class Is The . form // public class AlarmDriver {public static void Main (string [] args) {// instantiates the event receiver WakeMeUp w = new WakeMeUp ();. // instantiates the event source AlarmClock clock = new AlarmClock ().;

// Wires the alarmrang method to the alarm Event. Clock.Alarm = New AlarMeventHandler (W.AlarmRRAN); Clock.Start ();}}} [Visual Basic] 'EventSample.vb.'Option ExplicitOption Strict

Imports Systemimports System.comPonentModelimports Microsoft.visualBasic

Namespace EventSample 'Class that contains the data for' the alarm event. Derives from System.EventArgs. 'Public Class AlarmEventArgs Inherits EventArgs Private _snoozePressed As Boolean Private nrings As Integer' Constructor. 'Public Sub New (snoozePressed As Boolean, nrings As Integer) Me._snoozePressed = snoozePressed Me.nrings = nrings End Sub 'The NumRings property returns the number of rings' that the alarm clock has sounded when the alarm event 'is generated.' Public ReadOnly Property NumRings () As Integer Get Return nrings End Get End Property 'The SnoozePressed property indicates whether the snooze' button is pressed on the alarm when the alarm event is generated. 'Public ReadOnly Property SnoozePressed () As Boolean Get Return _snoozePressed End Get End Property' Th e AlarmText property that contains the wake-up message. 'Public ReadOnly Property AlarmText () As String Get If _snoozePressed Then Return "Wake Up !!! Snooze time is over." Else Return "Wake Up!" End If End Get End Property End class 'delegate Declaration.' Public Delegate Sub AlarMeventhandler (Sender As Object, e as alarmeventargs)

'The Alarm class that raises the alarm event.' Public Class AlarmClock Private _snoozePressed As Boolean = False Private nrings As Integer = 0 Private stopFlag As Boolean = False 'The Stop property indicates whether the' alarm should be turned off. 'Public Property [ Stop] () As Boolean Get Return stopFlag End Get Set stopFlag = value End Set End Property 'The SnoozePressed property indicates whether the snooze' button is pressed on the alarm when the alarm event is generated. 'Public Property SnoozePressed () As Boolean Get Return_snoozepressed End Get_Snoozepressed = Value End Set End Property

'The event member that is of type AlarmEventHandler.' Public Event Alarm As AlarmEventHandler 'The protected OnAlarm method raises the event by invoking' the delegates. The sender is always this, the current instance 'of the class.' Protected Overridable Sub OnAlarm ( e As AlarmEventArgs) RaiseEvent Alarm (Me, e) End Sub 'This alarm clock does not have' a user interface. 'To simulate the alarm mechanism it has a loop' that raises the alarm event at every iteration 'with a time delay of 300 milliseconds, 'if snooze is not pressed. If snooze is pressed,' the time delay is 1000 milliseconds. 'Public Sub Start () Do nrings = 1 If stopFlag Then Exit Do Else If _snoozePressed Then System.Threading.Thread.Sleep (1000) IF (True) THEN DIM E As New AlarmEventArgs (_snoozePressed, nrings) OnAlarm (e) End If Else System.Threading.Thread.Sleep (300) Dim e As New AlarmEventArgs (_snoozePressed, nrings) OnAlarm (e) End If End If Loop End Sub End Class' The Wakemeup Class That Has A Method Alarmrang That Handles The 'Alarm Event.' Public Class Wakemeup Public Sub Alarmrang (Sender As Object, e as alarmeventargs) Console.writeline ((E.AlarmText )

ControlChars.cr)) if NOTE.NUOZEPRESSED THEN IF E.NUMRINGS MOD 10 = 0 THEN Console.writeline ("Let alarm Ring? Enter Y" Console.Writeline ("Press Snooze? Enter N" Console.Writeline STOP ALARM? ENTER Q ") DIM INPUT AS STRING = Console.Readline () IF INPUT.EQUALS (" Y ") or Input.Equals (" y ") The return else if INPUT.EQUALS (" N ") or Input. Equals ( "n") Then CType (sender, AlarmClock) .SnoozePressed = True Return Else CType (sender, AlarmClock) .Stop = True Return End If End If End If Else Console.WriteLine ( "Let alarm ring? Enter Y") Console.writeline ("Stop Alarm? Enter Q") DIM INPUT AS STRING = Console.Readline () if INPUT.EQUALS ("Y" ) Or input.Equals ( "y") Then Return Else CType (sender, AlarmClock) .Stop = True Return End If End If End Sub End Class 'The driver class that hooks up the event handling method of' WakeMeUp to the alarm event of an Alarm object using a delegate. 'In a forms-based application, the driver class is the' form. 'Public class AlarmDriver Public Shared Sub Main ()' instantiates the event receiver. Dim w As New WakeMeUp () '

转载请注明原文地址:https://www.9cbs.com/read-104588.html

New Post(0)