Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Tool Which is Useful to Calculate the Office Work Hours

0.00/5 (No votes)
3 Jan 2015 2  
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.

C#
//
        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.

C#
//
        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.

C#
//
        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.

C#
//
        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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here