Click here to Skip to main content
15,905,912 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
i have stopwatch in windows application but i need to implement stopwatch in web application how can i do it i tried out

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.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Globalization;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;

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

        sw.Start();
    }

    

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        if (sw.IsRunning)
        {
            TimeSpan objTimeSpan = TimeSpan.FromMilliseconds(sw.ElapsedMilliseconds);
            Label1.Text = String.Format(CultureInfo.CurrentCulture, "{0:00}:{1:00}:{2:00}.{3:00}", objTimeSpan.Hours, objTimeSpan.Minutes, objTimeSpan.Seconds, objTimeSpan.Milliseconds / 10);

            if (pas)
            {
                pas = false;
            }
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        sw.Start();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        sw.Stop();
    }

    protected void Button3_Click1(object sender, EventArgs e)
    {
        Label1.Text = "00:00:00.00";
        sw.Reset();
    }
}

sw is not running when i click the button try to solve it


Thanks @ Regards by

Dineshkumar R
Posted
Updated 4-May-11 18:57pm
v2
Comments
Sergey Alexandrovich Kryukov 5-May-11 16:07pm    
A vote of 1 from me, just for the idea that the type of application might effect the functionality of fundamental classes like watch, etc. Ever heard of the separations of concerns?
--SA

You need to keep adding up the sw.ElapsedMilliseconds and storing them in a variable somewhere.
I guess this value is reset every time you start and stop.
 
Share this answer
 
Comments
R_D_K 5-May-11 2:14am    
how can i solve
Abhinav S 5-May-11 2:33am    
I'm not 100% sure if this code will compile as it is pseudo code. But give this a try.
Have a variable and store the time in it.

var x = sw.ElapsedMilliseconds;

Everytime you stop the timer using the stop button add the new elapsed time to x.
Display x in label1.
see this link

Tiny StopWatch Application[^]
 
Share this answer
 
Comments
R_D_K 5-May-11 3:00am    
i need source code try to help me
i am struggling for this issues for last 6 day
Ashishmau 5-May-11 3:27am    
Have u downloaded this and tried
Simple Example for Stopwatch

Here[^]
 
Share this answer
 

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