如何创建一个秒表计时 [英] How to create a stopwatch timer

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

问题描述

我有一个应该像一个秒表计时的asp.net应用程序。我试图就如何得到定时器的变化用秒计时。我用了一个算法,主要是效率不高。然后,我用的时间跨度对象使用日期时间的数学尝试过,但因为这基本上由一个时钟是不理想的。现在我想实现System.Diagnostics程序,所以我可以用一个秒表对象。当我运行我的程序,那应该是与时间更新标签只显示全部为0。

I have an asp.net application that is supposed to act like a stopwatch timer. I've tried variations on how to get the timer to count time using seconds. I used an algorithm that was largely inefficient. I then tried using timespan objects using datetime math, but since this basically made a clock it was undesireable. Now I'm trying to implement System.Diagnostics so I can use a Stopwatch object. When I run my program, the label that is supposed to update with the time just displays all 0's.

这是与计时器和更新面板中的.aspx页面中的code:

This is the code in the .aspx page with the the timer and update panel:

      <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <asp:Timer ID="Timer1" runat="server" Enabled="false" 
                            Interval="1000" OnTick="Timer1_Tick"></asp:Timer>
                        <asp:Label ID="Label1" runat="server"></asp:Label>
                    </ContentTemplate>
                </asp:UpdatePanel>

这是启动秒表和定时器为我的启动按钮事件:

This is the event for my start button that starts the stopwatch and the timer:

      protected void Start_Click(object sender, EventArgs e)
    {
        //Start the timer
        Timer1.Enabled = true;
        stopWatch.Start();
    }

这是定时器事件(currentTime的是一个时间跨度对象):

This is the event for the timer1 (currentTime is a timespan object):

      protected void Timer1_Tick(object sender, EventArgs e)
    {
        currentTime = stopWatch.Elapsed;
        Label1.Text = String.Format("{0:00}:{1:00}:{2:00}", 
        currentTime.Hours.ToString(), currentTime.Minutes.ToString(), 
        currentTime.Seconds.ToString());

老实说,我不知道我做错了。我想这将是使秒表计时最简单的方法,但没有在标签内改变。我想AP preciate的任何援助。如果有必要,我会提供更多的信息。

I honestly don't know what I'm doing wrong. I figured this would be the easiest way to make a stopwatch timer, but nothing is changing within the label. I would appreciate any assistance. I will provide more information if necessary.

推荐答案

试试下面的code。它为我工作。

Try the below code. It works for me.

添加下面的code在websource code:

Add the below code in websource code:

<asp:scriptmanager ID="Scriptmanager1" runat="server"></asp:scriptmanager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
  <ContentTemplate>
    <asp:Label ID="Label1" runat="server" Font-Size="XX-Large"></asp:Label>
    <asp:Timer ID="tm1" Interval="1000" runat="server" ontick="tm1_Tick" />
  </ContentTemplate>
  <Triggers>
    <asp:AsyncPostBackTrigger ControlID="tm1" EventName="Tick" />
  </Triggers>
</asp:UpdatePanel>

添加按照你的CS文件来源$ C ​​$ C:

Add following source code in your cs file:

using System.Diagnostics;

public partial class ExamPaper : System.Web.UI.Page
{
    public static Stopwatch sw;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            sw = new Stopwatch();
            sw.Start();
        }
    }

    protected void tm1_Tick(object sender, EventArgs e)
    {
        long sec = sw.Elapsed.Seconds;
        long min = sw.Elapsed.Minutes;

        if (min < 60)
        {
            if (min < 10)
                Label1.Text = "0" + min;
            else
                Label1.Text = min.ToString();

            Label1.Text += " : ";

            if (sec < 10)
                Label1.Text += "0" + sec;
            else
                Label1.Text += sec.ToString();
        }
        else
        {
            sw.Stop();
            Response.Redirect("Timeout.aspx");
        }
    }
} 

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

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