2

我想在预定义的时间后停止计时器。我正在制作在线考试申请,学生将无法在预定时间后继续考试。我已经实现了计时器,它运行良好,但我希望计时器应该在 1 小时 20 分钟后自动停止。所以我的问题是如何写那个条件和在哪里?我正在使用 ASP.net C#。我的示例代码是:

static DateTime dt;
int m_StartTime = Environment.TickCount;
long m_Interval = new TimeSpan(0, 1, 0).Ticks;

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        dt = new DateTime(2011, 1, 1, 0, 0, 0);
        Timer1.Enabled = false;
        Timer1.Interval = 1000;
    }
}
protected void Timer1_Tick(object sender, EventArgs e)
{
    dt = dt.AddSeconds(10);
    Label1.Text = dt.ToString("H:mm:ss");
    if (Environment.TickCount - m_StartTime > m_Interval)
    {
        Timer1.Enabled=false;            
    }  


}
protected void start_Click(object sender, EventArgs e)
{
    Timer1.Enabled = true;
}
4

4 回答 4

2

我认为您可以在启动计时器时存储滴答数,Environment.TickCount然后在Timer1_Tick您检查当前数字是否滴答时 - 存储的滴答数大于您的时间间隔。然后你停止计时器。你可以得到一个值来比较:new TimeSpan(1, 20, 0).Ticks

...
Timer1.Enabled = true;
m_StartTime = Environment.TickCount
m_Interval = new TimeSpan(1, 20, 0).Ticks;
...

protected void Timer1_Tick(object sender, EventArgs e)
{
    dt = dt.AddSeconds(1);
    Label1.Text = dt.ToString("H:mm:ss");    
    if (Environment.TickCount - m_StartTime > m_Interval)
    {
        Timer1.Stop();   
    }
}

你当然可以做一个常数而不是一个变量等等,这只是一个例子来说明这个想法。

PS 顺便说一句,您似乎只需要一个布尔 startstop 而不是字符串。

于 2013-07-01T08:27:31.560 回答
0

在 asp.net 中,计时器不是那么容易使用,我建议您阅读这篇 msdn-article。一般来说,我建议您为此目的使用第二个计时器,而不是为第一个计时器添加额外的逻辑。此外,您应该使用 enum 或 bool 作为 startstop。

于 2013-07-01T08:29:10.427 回答
0
public partial class _Default : System.Web.UI.Page
{
   static DateTime dt = new DateTime(2014,9,1,0,0,0,000);
   Timer t = new Timer();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Timer1.Enabled = false; 
            Timer1.Interval = 1;
            lbl.Text = dt.ToLongTimeString() + ":" + dt.Millisecond;
        }
    }

protected void Timer1_Tick(object sender, EventArgs e)
{
    dt = dt.AddMilliseconds(1);
    lbl.Text = dt.ToLongTimeString() + ":" + dt.Millisecond;
}


protected void Reset_Click(object sender, EventArgs e)
{
    Timer1.Enabled = false; //For stop the timer
    dt = new DateTime(2014, 9, 1, 0, 0, 0, 000);
    lbl.Text = dt.ToLongTimeString() + ":" + dt.Millisecond;

}
protected void Pause_Click(object sender, EventArgs e)
{
    Timer1.Enabled = false; //For stop the timer

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

}

于 2014-09-18T14:04:53.130 回答
0
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            dt = new DateTime(2011, 1, 1, 0, 0, 0);
            Timer1.Enabled = false;
            Timer1.Interval = 1000;
            Timer2.Enabled = false;
            Timer2.Interval = 1000 * 60 * MIN_PER_TEST; // set interval for a test, if it is changing, do this in Button1_click [startstop=="stop"] before setting Enabled = true
            startstop = "Stop";
        }
    }

    // end of the test came, do what you need to do in order to kill the student mood further more :)
    protected void Timer2_Tick(object sender, EventArgs e)
    {
        Button1_Click(sender, e); // simulate clicking STOP, though might want to replace that!
    }

    // Added start and stop for the test duration timer.
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (startstop == "Stop")
        {
            startstop = "Start";
            Timer1.Enabled = true;
            Timer2.Enabled = true;
            Button1.Text = "Stop";
        }
        else
        {
            startstop = "Stop";
            Timer1.Enabled = false;
            Timer2.Enabled = false;
            Button1.Text = "Start";
        }
    }

希望这会有所帮助。

于 2013-07-01T08:56:48.340 回答