Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当一次有多个用户登录时,如何防止计时器跳过asp.net在线考试项目?_C#_Asp.net - Fatal编程技术网

C# 当一次有多个用户登录时,如何防止计时器跳过asp.net在线考试项目?

C# 当一次有多个用户登录时,如何防止计时器跳过asp.net在线考试项目?,c#,asp.net,C#,Asp.net,我的问题是,我在asp.net在线考试网站上使用了计时器。 一个用户登录尝试测试我的计时器,如预期的那样减少时间值,但多个用户尝试测试我的计时器跳过滴答声的时间,并且它运行得非常快。我不知道原因是什么 我的code.cs是 protected void timer1_tick(object sender, EventArgs e) { if (TimeAllSecondes > 0) { TimeAllSecondes = TimeAllSe

我的问题是,我在asp.net在线考试网站上使用了计时器。 一个用户登录尝试测试我的计时器,如预期的那样减少时间值,但多个用户尝试测试我的计时器跳过滴答声的时间,并且它运行得非常快。我不知道原因是什么

我的code.cs是

protected void timer1_tick(object sender, EventArgs e)
{        
    if (TimeAllSecondes > 0)
    {
        TimeAllSecondes = TimeAllSecondes - 1;
        if (TimeAllSecondes < 0)
        {
            Session["endtime"] = DateTime.Now;
            //Session["SubjectID3"] = txtsubid.Value;
            //Session["SubjectName3"] = txtsubname.Value;
            mcqmethod.update_astatus(Convert.ToString(Session["empid"]), Convert.ToInt32(Session["EXAMID"]));
            Response.Redirect("~/MCQ/students/s_thankyou.aspx");

        }
    }

    TimeSpan time_Span = TimeSpan.FromSeconds(TimeAllSecondes);
    hh = time_Span.Hours;
    mm = time_Span.Minutes;
    ss = time_Span.Seconds;
    lblTimer.Text = " Your exam time is remaining :  " + hh + ":" + mm + ":" + ss + "[H:M:S]";
}

<asp:Timer ID="timer1" runat="server" Interval="1000" OnTick="timer1_tick" ></asp:Timer>

您不应该在asp.net应用程序中使用特定于用户的静态字段,因为它们在所有用户之间共享。1

同样,您不能使用实例字段,因为页面实例仅持续单个请求/响应生命周期的生命周期

你想要的是与单个用户隔离的东西,其生命周期与用户在你的站点上花费的时间相同

这就是,您应该存储
timeallseconds
值的位置



1这掩盖了许多实例也会失去状态的情况,例如当应用程序池回收时,或者当您运行多个流程实例时。但简而言之,它们没有正确的隔离和生命周期语义。

我立即怀疑
TimeAllSecondes
。但是您还没有向我们展示它的定义位置。好的,我肯定会展示oneDateTime examdate=Convert.ToDateTime(会话[“examdate”])//考试日期时间endexam=examdate.AddMinutes(Convert.ToDouble(Session[“examtime”]);会话[“starttime”]=DateTime.Now;TimeSpan remingtime=endexam.Subtract(DateTime.Now);TimeAllSecondes=remingtime.TotalSeconds;我没有在任何地方存储该值,首先我获取所有秒数,然后我设置了一个计时器每秒限制,然后它每秒钟自动滴答一次。@user3296718-您需要存储它,并且需要在会话中存储它,就像
empid
和其他任何你想与其他用户隔离并延长生存期的东西一样。@user3296718-你得到“跳过”的原因是,对于四个用户,每秒钟有四个计时器在运行,它们都对同一个变量运行相同的逻辑,所以每秒下降四个(大约)非常感谢..我已经尝试了您告诉我的,现在我得到了结果计时器可以与多个用户一起正常工作
static double TimeAllSecondes;

public void getexam()
{
    DataTable dt = new DataTable();
    dt = mcqmethod.getexambysub(Convert.ToInt32(Session["EXAMID"]),Convert.ToString(Session["empid"]));
    lblsubject.Text = Convert.ToString(dt.Rows[0]["subname"]);
    lbltime.Text = Convert.ToString(dt.Rows[0]["etime"]);
    noofquestion = Convert.ToInt32(dt.Rows[0]["t_n_q"]);
    Session["ExamDate"] = dt.Rows[0]["e_date"];
    Session["examtime"] = Convert.ToString(dt.Rows[0]["etime"]);//Minutes of exam
    DateTime examdate = Convert.ToDateTime(Session["ExamDate"]);//date of exam
    DateTime  endexam = examdate.AddMinutes(Convert.ToDouble(Session["examtime"]));

    Session["starttime"] = DateTime.Now;
    TimeSpan remingtime = endexam.Subtract(DateTime.Now);       
    TimeAllSecondes = remingtime.TotalSeconds;         
    Session["noq"] = Convert.ToInt16(dt.Rows[0]["t_n_q"]);
    btnprev.Visible = false;
}