Click here to Skip to main content
15,889,861 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to develop a work status application using asp.net with c#

In this Project i want to calculate the break time.

I think stopwatch is best way to calculate the time.

In this project i have 2 buttons

1 start break
2 stop break

if the button 1 click stop watch want to start
if the button 2 click stop watch want to pass

how can i implement

Please help me

thanks in advance
Dineshkumar R
Posted
Updated 11-May-11 2:31am
v3

 
Share this answer
 
Hope A High-Precision Stopwatch for C#[^] article from CP might help you.
 
Share this answer
 
Implement below class in your project.

public class StopWatch
    {
        private DateTime startTime;
        private DateTime stopTime;
        private bool running = false;

        public void Start()
        {
            this.startTime = DateTime.Now;
            this.running = true;
        }

        public void Stop()
        {
            this.stopTime = DateTime.Now;
            this.running = false;
        }

        // elaspsed time in milliseconds
        public double GetElapsedTime()
        {
            TimeSpan interval;
            if (running)
                interval = DateTime.Now - startTime;
            else
                interval = stopTime - startTime;
            return interval.TotalMilliseconds;
        }

        // elaspsed time in seconds
        public double GetElapsedTimeSecs()
        {
            TimeSpan interval;
            if (running)
                interval = DateTime.Now - startTime;
            else
                interval = stopTime - startTime;
            return interval.TotalSeconds;
        }

        
    }


Now create instance (Page Level Variable) of StopWatch Class.
StopWatch myWatch = new StopWatch();


And call myWatch.Start(); in button1_click and myWatch.Stop(); in button2_click.

Use myWatch.GetElapsedTimeSecs().ToString(); to display/get elapsed time.


In your ASPX code behind just add below changes

C#
public partial class _Default : System.Web.UI.Page
    {
        static StopWatch sw;
        //TimeSpan tt1 = TimeSpan.Zero;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
                sw = new StopWatch();
        }


Reason: every time in clicking on button it creates new instance of sw object. We are checking here for is not postback.
Regards
Rushi
 
Share this answer
 
v2
Comments
R_D_K 11-May-11 9:09am    
here my coding still is not working find it
C# Coding
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Windows.Forms;
using System.Diagnostics;

public partial class _Default : System.Web.UI.Page
{
Stopwatch sw = new Stopwatch();
TimeSpan tt1 = TimeSpan.Zero;
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
sw.Start();
Button1.Enabled = false;
TextBox1.Text = System.DateTime.Now.ToString();
Button2.Enabled = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
sw.Stop();
Button2.Enabled = false;
Button1.Enabled = true;
TextBox2.Text = System.DateTime.Now.ToString();
DateTime d1 = Convert.ToDateTime(TextBox1.Text);
DateTime d2 = Convert.ToDateTime(TextBox2.Text);
System.TimeSpan diff = d2.Subtract(d1);
tt1 = diff.Add(tt1);
TextBox3.Text = tt1.ToString();
//System.TimeSpan diff2 = diff.Add(diff1);
MessageBox.Show(sw.Elapsed.ToString());




}
public class StopWatch
{
private DateTime startTime;
private DateTime stopTime;
private bool running = false;

public void Start()
{
this.startTime = DateTime.Now;
this.running = true;
}

public void Stop()
{
this.stopTime = DateTime.Now;
this.running = false;
}

// elaspsed time in milliseconds
public double GetElapsedTime()
{
TimeSpan interval;
if (running)
interval = DateTime.Now - startTime;
else
interval = stopTime - startTime;
return interval.TotalMilliseconds;
}

// elaspsed time in seconds
public double GetElapsedTimeSecs()
{
TimeSpan interval;
if (running)
interval = DateTime.Now - startTime;
else
interval = stopTime - startTime;
return interval.TotalSeconds;
}

}
}
ASP.Net Coding

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:TextBox ID="TextBox1" runat="server" ReadOnly="True">
<asp:TextBox ID="TextBox2" runat="server" ReadOnly="True">
<asp:TextBox ID="TextBox3" runat="server" ReadOnly="True">
<asp:Button ID="Button1" runat="server" AccessKey="A" onclick="Button1_Click"
Text="Button" />
<asp:Button AccessKey="M" ID="Button2" runat="server" onclick="Button2_Click"
Text="Button" Enabled="False" />
<asp:Button ID="Button3" runat="server" Text="Button" AccessKey="N"
Visible="False" />
<asp:TextBox ID="TextBox4" runat="server" Visible="False">00:00:00

</div>
</form>
</body>
</html>
Joshi, Rushikesh 11-May-11 10:12am    
In your ASPX code behind just add below changes

<pre>
public partial class _Default : System.Web.UI.Page
{
static StopWatch sw;
//TimeSpan tt1 = TimeSpan.Zero;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
sw = new StopWatch();
} </pre>

Reason: every time in clicking on button it creates new instance of sw object. We are checking here for is not postback.

Regards
Rushi
Member 14005734 22-Oct-18 4:06am    
is this working correctly or not
R_D_K 12-May-11 2:00am    
Tks rushijoshi

but i have another one problem

i want to display the total break time

user can take more than one break per day. So i want to calculate total break time for this what i want to do
in stop watch pass method is available

regards,
Dineshkumar R
Member 14005734 22-Oct-18 4:06am    
s this working correctly or not

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900