locked
session variable is not updating RRS feed

  • Question

  • User641655154 posted

    I'm trying to  develop a progress bar that shows  the status of server side process..

    Im facing a problem that my session variable is not updating....

    this is my code

    private void LongTask()
    {

    for (int i = 0; i < 11; i++)
    {
    System.Threading.Thread.Sleep(1000);
    Session["State"] = i + 1;
    }
    Session["State"] = 100;

    }

    public void Button1_Click(object sender, System.EventArgs e)
    {
    Thread thread = new Thread(new ThreadStart(LongTask));
    thread.Start();

    Session["State"] = 1;// initialize session variable
    OpenProgressBar(this.Page);
    }

    public static void OpenProgressBar(System.Web.UI.Page Page)
    {
    StringBuilder sbScript = new StringBuilder();

    sbScript.Append("<script language='JavaScript' type='text/javascript'>\n");
    sbScript.Append("<!--\n");
    sbScript.Append("window.showModalDialog('Progress.aspx','','dialogHeight: 150px; dialogWidth: 350px; edge: Raised; center: Yes; help: No; resizable: No; status: No;scroll:No;');\n");
    sbScript.Append("// -->\n");
    sbScript.Append("</script>\n");
    Page.RegisterClientScriptBlock("OpenProgressBar", sbScript.ToString());
    }

     and inside Progress.aspx ..

    private int state = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
    if (Session["State"] != null)
    {
    state = Convert.ToInt32(Session["State"].ToString());
    }
    else
    {
    Session["State"] = 0;
    }
    if (state > 0 && state <= 10)
    {
    this.lblMessages.Text = "Processing...";
    this.panelProgress.Width = state * 30;
    this.lblPercent.Text = state * 10 + "%";
    //Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "", "<script>window.setTimeout('window.theForm.submit()',100);</script>");
    Page.RegisterStartupScript("", "<script>window.setTimeout('window.form1.submit()',100);</script>");
    }
    if (state == 100)
    {
    this.panelProgress.Visible = false;
    this.panelBarSide.Visible = false;
    this.lblMessages.Text = "Task Completed!";
    Page.RegisterStartupScript("", "<script>window.close();</script>");
    }

    }

    here session variable is intilizingg but not updatingg as in the LongTask function

    so im gettting session value as "1 " in each page load of progress.aspx

    Thursday, March 5, 2015 9:44 AM

Answers

  • User1644755831 posted

    Hi dson,

    protected void Page_Load(object sender, EventArgs e)
    {
    if (Session["State"] != null)
    {
    state = Convert.ToInt32(Session["State"].ToString());
    }
    else
    {
    Session["State"] = 0;
    }
    if (state > 0 && state <= 10)
    {
    this.lblMessages.Text = "Processing...";
    this.panelProgress.Width = state * 30;
    this.lblPercent.Text = state * 10 + "%";
    //Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "", "<script>window.setTimeout('window.theForm.submit()',100);</script>");
    Page.RegisterStartupScript("", "<script>window.setTimeout('window.form1.submit()',100);</script>");
    }
    if (state == 100)
    {
    this.panelProgress.Visible = false;
    this.panelBarSide.Visible = false;
    this.lblMessages.Text = "Task Completed!";
    Page.RegisterStartupScript("", "<script>window.close();</script>");
    }

    }

    You haven't put any kind of loop that will update the progress bar value after page loading.

    You need to read State from session variable regularly. Please do something like this

    protected void Page_Load(object sender, EventArgs e)
     {
     
    do while(Convert.ToInt32(Session["State"].ToString()) != 100)
    {
    var state = Convert.ToInt32(Session["State"].ToString());
     if (state > 0 && state <= 10)
     {
    this.lblMessages.Text = "Processing...";
    this.panelProgress.Width = state * 30;
    this.lblPercent.Text = state * 10 + "%";
     //Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "", "<script>window.setTimeout('window.theForm.submit()',100);</script>");
    Page.RegisterStartupScript("", "<script>window.setTimeout('window.form1.submit()',100);</script>");
     }
     }
    if (state == 100)
     {
    this.panelProgress.Visible = false;
    this.panelBarSide.Visible = false;
    this.lblMessages.Text = "Task Completed!";
    Page.RegisterStartupScript("", "<script>window.close();</script>");
     }
    }
    

    Hope this helps,

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 6, 2015 1:09 AM