如何使用秒表 [英] How to use Stopwatch

查看:118
本文介绍了如何使用秒表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows应用程序中有秒表,但是我需要在Web应用程序中实现秒表,我该怎么做呢?

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没有运行


感谢@问候by

Dineshkumar R


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


Thanks @ Regards by

Dineshkumar R

推荐答案

您需要不断添加sw.ElapsedMilliseconds并将它们存储在某个变量中.
我猜每次启动和停止时都会重置此值.
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.


请参阅此链接

微型秒表应用程序 [
see this link

Tiny StopWatch Application[^]


秒表的简单示例

此处 [
Simple Example for Stopwatch

Here[^]


这篇关于如何使用秒表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆