using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
 
namespace Events
{
    // Event Publisher
        public class DelegateEvent
        { 
            public delegate void AttendanceLogHandler(string Message); 
 
            // Define an Event based on the above Delegate
            public event AttendanceLogHandler EventLog; 
 
            public void LogProcess()
            {
                string Reason = null;                
      Console.WriteLine("Enter your name");
                string UserName = Console.ReadLine();
                DateTime t = DateTime.Now;
                int hr = t.Hour;
                int m = t.Minute;
                if (!(hr >= 9 && hr < 10 || (hr == 10 && m == 0)))
                {
                    Console.WriteLine("Enter the reason for not coming within the valid time:");
                    Reason = Console.ReadLine();
                }
 
                OnEventLog("Logging the info :" + UserName);
                if (hr >= 9 && hr < 10 || (hr == 10 && m == 0))
                    if (hr < 10)
                        OnEventLog(" Logged in at " + hr.ToString() + ":0" + m.ToString() + " Within Time");
                    else
      
                 OnEventLog(" Logged in at " + hr.ToString() + ":" + m.ToString() + " Within Time");
                else
                    if (hr > 10)
                        OnEventLog(" Logged in at " + hr.ToString() + ":0" + m.ToString() + " not Within Time because " + Reason);
                    else
                        OnEventLog(" Logged in at " + hr.ToString() + ":" + m.ToString() + " not Within Time because " + Reason);
                OnEventLog(" ");
            }
 
            // By Default, create an OnZZZZ Method, to call the Event
            protected void OnEventLog(string Message)
            {
                if (EventLog != null)
                {
                    EventLog(Message);
                }
            }
        }
 
        // The AttendanceLogger class merely encapsulates the file I/O
        public class AttendanceLogger
        {
            FileStream FileStr;
            StreamWriter StreamWrtr;
 
            // Constructor
            public AttendanceLogger()
 
          {
                FileStr = new FileStream("process4.txt"FileMode.Append, FileAccess.Write);
                StreamWrtr = new StreamWriter(FileStr);
            }
 
            // Member Function which is used in the Delegate
            public void Logger(string LogInfo)
            {
                StreamWrtr.WriteLine(LogInfo);
            }
 
            public void Close()
            {
                StreamWrtr.Close();
                FileStr.Close();
            }
        }
 
        //Subscriber of the Event
        public class RecordAttendance
        {
            static void Logger(string LogInfo)
            {
                Console.WriteLine(LogInfo);
            }
 
            static void Main(string[] args)
            {
 
                AttendanceLogger FileLog = new AttendanceLogger();
                DelegateEvent DEvent = new DelegateEvent();
 
                // Subscribe the Functions Logger and FileLog.Logger
                DEvent.EventLog += new DelegateEvent.AttendanceLogHandler(Logger);
                DEvent.EventLog += new DelegateEvent.AttendanceLogHandler(FileLog.Logger);
 
                // The Event will now be triggered in the LogProcess() Method
                DEvent.LogProcess();
                Console.ReadLine();
                FileLog.Close();
            }
        }
    }
 

 Here is Output……

 

Events With Delegate


  Modified On Nov-29-2017 06:06:29 PM

Leave Comment