C# 如何让计时器等待完成?
我有一个ASP.NET网站,在那里我必须在C# 如何让计时器等待完成?,c#,asp.net,timer,updatepanel,C#,Asp.net,Timer,Updatepanel,我有一个ASP.NET网站,在那里我必须在网格视图上显示一些数据,我需要尽快显示这些数据,因此我决定在更新面板中创建一个计时器,然后一遍又一遍地刷新网格,但是我看到我的计时器并没有等到它完成后才能再次勾选,它一次又一次地执行,这给了我数据库的性能问题,我如何告诉我的计时器“嘿,停止,直到这个过程完成,然后继续” 刷新网格时,可以设置一个私有布尔变量,指示网格正在刷新,并且在执行刷新网格的代码之前,可以检查此变量 编辑-尝试使用会话变量而不是专用变量参见更新的示例。 示例- // code cha
网格视图上显示一些数据,我需要尽快显示这些数据,因此我决定在更新面板中创建一个计时器,然后一遍又一遍地刷新网格,但是我看到我的计时器并没有等到它完成后才能再次勾选,它一次又一次地执行,这给了我数据库的性能问题,我如何告诉我的计时器“嘿,停止,直到这个过程完成,然后继续”
刷新网格时,可以设置一个私有布尔变量,指示网格正在刷新,并且在执行刷新网格的代码之前,可以检查此变量
编辑-尝试使用会话变量而不是专用变量参见更新的示例。
示例-
// 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.
}
注意-我还没有测试代码,但它应该能给出需要做什么的合理想法。我试过了,但没用,它还在用。@user3044096,请将所有必要的信息添加到问题中,包括你试过但没用的内容。谢谢,我最后做了,但我用视图状态做了,谢谢!(我想我们都有一分钟忘记了asp.net是无状态的)请在问题中添加Timer1\u Tick
中的代码,同时添加您已经尝试过的内容。好的,Anchit,我添加了它。请参阅我编辑的答案。
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.
}