Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / C#
Tip/Trick

A Tool Which is Useful to Calculate the Office Work Hours

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
3 Jan 2015CPOL 16.1K   628   5   5
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
United States United States
Experienced Lead developer having 17+ years with a passion for developing innovative programs that expedite the efficiency and effectiveness of organizational success. Well-versed in technology and writing code to create systems that are reliable and user-friendly. A skilled leader who has the proven ability to motivate, educate, and manage a team of professionals to build software programs and effectively track changes. Confident communicator, strategic thinker, and innovative creator to develop software that is customized to meet a company’s organizational needs and further its success.

Comments and Discussions

 
QuestionNot displaying the Beak Time Pin
Praveena P K21-Dec-17 0:43
Praveena P K21-Dec-17 0:43 
QuestionNot displaying the Beak Time Pin
ANIL VIDYA SAGAR S7-Jan-15 19:34
ANIL VIDYA SAGAR S7-Jan-15 19:34 
GeneralNice tool Pin
itsgkiran6-Jan-15 7:03
itsgkiran6-Jan-15 7:03 
Nice tool Rajesh. Keep adding these type of tools.


Thanks,
Rama Kumar Dakka
Questionexe is not able to download. Pin
N Srinivas 20134-Jan-15 19:59
N Srinivas 20134-Jan-15 19:59 
AnswerRe: exe is not able to download. Pin
Rajesh Buddaraju4-Jan-15 20:03
Rajesh Buddaraju4-Jan-15 20:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.