[英]How do I make timer wait until done?
我有一個ASP.NET網站,必須在Gridview
上顯示一些數據,我需要盡快顯示這些數據,因此我決定在Update面板中創建一個計時器,然后一遍又一遍地刷新網格,但是我看到我的計時器沒有等到完成才能再次Tick
,它不斷地執行,這給了我數據庫性能問題,我怎么能告訴我的計時器“嘿,直到這個過程完成為止,然后繼續”。
<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>
我嘗試了這個:
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;
}
刷新網格時,可以設置一個私有布爾變量,以指示網格正在刷新,並且在執行刷新網格的代碼之前,可以檢查此變量。
編輯-嘗試使用會話變量而不是私有變量。 請參閱更新的示例。
范例-
// 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.
}
注意-我尚未測試代碼,但是應該對需要做的事情有一個清晰的了解。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.