65.9K
CodeProject is changing. Read more.
Home

A Tool Which is Useful to Calculate the Office Work Hours

starIconstarIconstarIconstarIconstarIcon

5.00/5 (1 vote)

Jan 3, 2015

CPOL
viewsIcon

16882

downloadIcon

631

A simple tool which is useful to calculate our day to day work hours

Introduction

How to capture employee work hours using C#.NET?

This tool is used to calculate the actual work hours of an employee. It displays the employee work time based on the system locks / unlocks and the login time.

Using the Code

The class ("SessionSwitchEventHandler") is used to log the break time based on the system lock and unlock.

The DateTime class is used to store the various dates like login and break times.

//
        private static SessionSwitchEventHandler sseh;
        public static DateTime LoginTime = DateTime.Now;
        public static DateTime LockTime;
        public static DateTime UnLockTime;
        public static TimeSpan TotalBreakTime;        
        static bool switchOn = false;
//

Add a timer control which is used to display the Total time spent in the office hours and the current time.

The TotalMilliseconds is used to get the total number of milli seconds from the login time and the current time.

The Invalidate function is used to redraw the control like a control refresh.

//
        private void timer1_Tick(object sender, EventArgs e)
        {                                   
            lblCurrentTime.Text = DateTime.Now.ToString();            
            lblCurrentTime.Invalidate();
            TimeSpan t = TimeSpan.FromMilliseconds
            ((DateTime.Now - TotalBreakTime - LoginTime).TotalMilliseconds);
            lblFinalTime.Text = string.Format("{0:D2}h:{1:D2}m:{2:D2}s:{3:D3}ms",
                            t.Hours,
                            t.Minutes,
                            t.Seconds,
                            t.Milliseconds);
            lblFinalTime.Invalidate();
        }
//

The method SystemEvents_SessionSwitch fires when the system locks and unlocks. In this method, the breaktime is displayed.

//
        void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
        {
            LockTime = DateTime.Now;
            if (!switchOn)
            {                
                UnLockTime = LockTime;
            }
            else
            {
                lblTotalTime.Text = LoginTime.ToString();
                lblTotalTime.Invalidate();
                TotalBreakTime = TotalBreakTime+(LockTime - UnLockTime);                
                lblBreakTime.Text = TotalBreakTime.ToString();
                lblBreakTime.Invalidate();
            }
            switchOn = true;                        
        }
//

The "SessionSwitchEventHandler" object is assigned to handle the "SessionSwith" events.

//
        private void Form1_Load(object sender, EventArgs e)
        {
            sseh = new SessionSwitchEventHandler(SystemEvents_SessionSwitch);
            SystemEvents.SessionSwitch += sseh;            
            lblTotalTime.Text = LoginTime.ToString();
            lblTotalTime.Invalidate();
        }
//

Points of Interest

The tool is very much useful for our daily usage and the class SessionSwitchEventHandler is used to handle the system events like lock/unlock, etc.