Question

I have an ASP.NET website where I have to show some data on a Gridview, I need this data to be shown as fast as possible, so I decided to create a timer inside an Update panel and then Refresh the grid over and over, however I see that my timer is not waiting until it was done in order to Tick again, it's executing itself over and over, which is giving me performance issues on the database, how can I tell my timer "hey stop until this process is done, then continue".

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
     <ContentTemplate>
            <asp:Timer ID="Timer1" runat="server" Interval="400" OnTick="Timer1_Tick" EnableViewState="False">
            </asp:Timer>
            <asp:GridView ID="gv_stats" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" ShowHeaderWhenEmpty="True" GridLines="Vertical" Width="562px" OnRowDataBound="gv_stats_RowDataBound" ShowFooter="True" EnableViewState="False" >
                   <AlternatingRowStyle BackColor="#CCCCCC" />
                        <Columns>
                        </Columns>
            </asp:GridView>
     </ContentTemplate>
</asp:UpdatePanel>

I tried this:

     private bool is_refreshing = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
        }
    }

    protected void Timer1_Tick(object sender, EventArgs e)
    {
        Timer1.Enabled = false; 
        if(is_refreshing == false)
        BindGrid();
        Timer1.Enabled = true; 



    }

   public void BindGrid()
        {

            is_refreshing = true;
            grd.datasource = con.executedt;
            grd.databind();
            is_refreshing = false;
         }
Was it helpful?

Solution

When you refresh the grid, you can set a private boolean variable indicating the that the grid is refreshing and before the execution of code that refreshes the grid, you can check this variable.

EDIT - Try using a session variable instead of a private variable. See updated example.

Example -

// code change starts
private bool _isGridRefreshing
{
   get
   {
      var flag = HttpContext.Current.Session["IsGridSession"];
      if(flag != null)
      {
         return (bool)flag;
      }

      return false;
   }
   set
   {
      HttpContext.Current.Session["IsGridSession"] = value;
   }
}
// code change ends

protected void Timer1_Tick(object sender, EventArgs e)
{
   if(_isGridRefreshing == false)
   {
       RefreshGrid();
   }
}

private void RefreshGrid()
{
   _isGridRefreshing = true;

   //code to refresh the grid.
}

Note - I haven't tested the code, but it should give a fair idea of what needs to be done.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top