Tags

Just like to post code for event handling for VB .NET and C# for reference
VB .NET

Imports System

Public Class CTimer

Delegate Sub SecondDel(ByVal xintTime As Integer)
Private evtSecond As SecondDel
Public Event evtMinute As SecondDel
Public Event evtHour(ByVal xHour As Integer)
public Shared lngSeconds As Long

Public Sub Register(ByVal objSecond As SecondDel)
evtSecond = evtSecond.Combine(evtSecond, objSecond)
End Sub

Public Sub OnTimer()
lngSeconds = lngSeconds + 1
If lngSeconds Mod 5 = 0 Then
evtSecond(lngSeconds)
End If
If lngSeconds Mod 10 = 0 Then
RaiseEvent evtMinute(lngSeconds)
End If
If lngSeconds Mod 30 = 0 Then
RaiseEvent evtHour(lngSeconds)
End If
End Sub

End Class

Public Class CClock

Private WithEvents mobjTimer As CTimer

Sub New()
mobjTimer = New CTimer()
mobjTimer.Register(New CTimer.SecondDel(AddressOf SecondEvent))
AddHandler mobjTimer.evtMinute, AddressOf MinuteEvent
While (mobjTimer.lngSeconds < 60) mobjTimer.OnTimer() System.Threading.Thread.Sleep(100) End While End Sub Private Sub SecondEvent(ByVal xintTime As Integer) Console.WriteLine("Second's Event") End Sub Private Sub MinuteEvent(ByVal xintTime As Integer) Console.WriteLine("Minute's Event") End Sub Private Sub mobjTimer_evtHour(ByVal xintTime As Integer) _ Handles mobjTimer.evtHour Console.WriteLine("Hour's Event") End Sub Public Shared Sub Main() Dim cc1 = New CClock() End Sub End Class [/sourcecode] C# [sourcecode language='csharp'] using System; public class CTimer { public delegate void SecondDel(int xintTime); private SecondDel evtSecond; public event SecondDel evtMinute; public event evtHourEventHandler evtHour; public delegate void evtHourEventHandler(int xHour); public static long lngSeconds; public void Register(SecondDel objSecond) { evtSecond = evtSecond.Combine(evtSecond, objSecond); } public void OnTimer() { lngSeconds = lngSeconds + 1; if (lngSeconds % 5 == 0) { evtSecond(lngSeconds); } if (lngSeconds % 10 == 0) { if (evtMinute != null) { evtMinute(lngSeconds); } } if (lngSeconds % 30 == 0) { if (evtHour != null) { evtHour(lngSeconds); } } } } public class CClock { private CTimer mobjTimer; public CClock() { mobjTimer = new CTimer(); mobjTimer.Register(new CTimer.SecondDel(SecondEvent)); mobjTimer.evtMinute += MinuteEvent; while ((mobjTimer.lngSeconds < 60)) { mobjTimer.OnTimer(); System.Threading.Thread.Sleep(100); } } private void SecondEvent(int xintTime) { Console.WriteLine("Second's Event"); } private void MinuteEvent(int xintTime) { Console.WriteLine("Minute's Event"); } private void mobjTimer_evtHour(int xintTime) { Console.WriteLine("Hour's Event"); } public static void Main() { object cc1 = new CClock(); } } [/sourcecode]

Advertisements