在预定义的时间后在asp.net c#中停止计时器

时间:2013-07-01 08:23:55

标签: c# asp.net

我想在预定义的时间后停止计时器。我正在进行在线考试申请,学生将无法在预定时间后继续考试。我已经实现了计时器,它运行良好,但我希望计时器应该在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 个答案:

答案 0 :(得分: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();   
    }
}

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

P.S。顺便说一句,你似乎只需要一个布尔启动码而不是一个字符串。

答案 1 :(得分:0)

在asp.net中,我建议您不要使用易于使用的计时器 msdn-article。 一般情况下,我会建议您为此目的使用第二个计时器,而不是为第一个计时器添加额外的逻辑。你也应该使用enum或bool作为startstop。

答案 2 :(得分: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";
        }
    }

希望这有用。

答案 3 :(得分: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
}

}