【问题标题】:ASP.NET Timer do not work. How to call function every X seconds?ASP.NET 计时器不起作用。如何每 X 秒调用一次函数?
【发布时间】:2020-07-13 04:00:00
【问题描述】:

我正在 ASP.NET Framework 中创建一个页面,我需要每 X 秒更新一次 Label 中的文本。我尝试创建新的Thread 并调用Thread.Sleep(),但它不起作用,下面写的所有内容 Thread.Sleep 不执行,我不知道为什么。所以我在互联网计时器上找到了。我创建了 Timer,它不调用方法。如果我将它静态写入 .aspx 它可以工作:

 <asp:ScriptManager ID="ScriptManager1" runat="server" />
     <asp:Timer runat="server" id="UpdateTimer" interval="5000" ontick="UpdateTimer_Tick" />
    <asp:UpdatePanel runat="server" id="TimedPanel" updatemode="Conditional">
        <Triggers>
            <asp:AsyncPostBackTrigger controlid="UpdateTimer" eventname="Tick" />
        </Triggers>
        <ContentTemplate>
            <asp:Label runat="server" id="DateStampLabel" />
        </ContentTemplate>
    </asp:UpdatePanel>
  protected void UpdateTimer_Tick(object sender, EventArgs e)
    {
        DateStampLabel.Text = DateTime.Now.ToString();
    }

这可行,但我想更改时间,所以我在 C# 中动态创建它:

  if (!IsPostBack)
        {
            Timer timer = new Timer();
            timer.Tick += UpdateTimer_Tick;
            timer.Interval = 2000;
            timer.Enabled = true;
        }

但是UpdateTimer 从未被调用过。所以我尝试了这个,它仍然不起作用:

 protected override void OnInit(EventArgs e)
    {
        if (!IsPostBack)
        {
            Timer timeoutTimer = new Timer();
            timeoutTimer.ID = "timeouttimer";
            timeoutTimer.Interval = 10000;
            timeoutTimer.Tick += new EventHandler<EventArgs>(UpdarteTimer_Tick);


            UpdatePanel timerUpdatePanel = new UpdatePanel();
            timerUpdatePanel.ContentTemplateContainer.Controls.Add(timeoutTimer);


            ScriptManager.GetCurrent(this.Page).RegisterAsyncPostBackControl(timeoutTimer);


            this.Page.Form.Controls.Add(timerUpdatePanel);
        }


        base.OnInit(e);
    }

我的网站上真的需要这个。请帮我。如何在 ASP.NET 中创建一些例程?为什么我不能在网站中使用 Thread.Sleep 以及为什么我的计时器不起作用?谢谢

【问题讨论】:

  • 你有没有考虑过做这个客户端(在 JS 中)?
  • @SalahAkbari 谢谢,但它不起作用,方法在页面启动时只调用了一次
  • @mjwills 我不懂JS
  • 我强烈建议花一些时间学习它。

标签: c# asp.net .net web


【解决方案1】:

如果您只是想更新页面上计时器的间隔..您可以引用已经创建的间隔并像这样在 Page_Init 或 Page_Load 中设置间隔...

       <asp:ScriptManager ID="ScriptManager1" runat="server" />
     <asp:Timer runat="server" id="UpdateTimer" interval="5000" ontick="UpdateTimer_Tick"  />
    <asp:UpdatePanel runat="server" id="TimedPanel" updatemode="Conditional">
        <Triggers>
            <asp:AsyncPostBackTrigger controlid="UpdateTimer" eventname="Tick" />
        </Triggers>
        <ContentTemplate>
            <asp:Label runat="server" id="DateStampLabel" />
        </ContentTemplate>
    </asp:UpdatePanel>

 protected void Page_Load(object sender, EventArgs e)
 {
     if (!IsPostBack)
        {
            UpdateTimer.Interval = 2000;
        }
 }

【讨论】:

    【解决方案2】:

    如果可以的话,可以使用js代码

    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <script type="text/javascript">
    
        $(document).ready(function () {
            setInterval(function () {
                WebForm_GetElementById("MainContent_Label1").textContent = new Date().toLocaleString();
            }, 1000);
        });
    
    </script>
    

    请注意,您要查找的标签 ID 和元素名称是不同的! (老实说,我不知道为什么 ASP.NET 会更改该 ID)

    【讨论】:

      【解决方案3】:

      设置计时器属性后,您必须调用此行来启动计时器:

      timer.Start();
      

      统一更新: 我检查了你的 UI 计时器,它可以工作,但是每次发生滴答事件时它都会重新加载页面。

      <asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick"></asp:Timer>
      

      在文件后面的代码中:

          protected void Page_Load(object sender, EventArgs e)
          {
              Timer1.Interval = 1000;
          }
      
          protected void Timer1_Tick(object sender, EventArgs e)
          {
              Label1.Text = DateTime.Now.ToString();
          }
      

      因此,我建议使用我在另一个答案中提到的js方法。

      【讨论】:

      • 谢谢,我知道我必须调用 Start(),但是 Timer 不包含方法 Start()
      • 您使用哪个计时器?来自哪个图书馆?
      • 从System.Web.UI,我知道还有System.Timers.Timer,但它有不同的构造函数,不包含Tick
      • 我复制了你的代码,并在下面的评论中添加了这个:Label1.Text = DateTime.Now.ToLongTimeString();,但是 TimerTick() 只在页面加载时被调用过一次,然后没有
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-22
      • 2017-10-28
      • 2019-04-08
      • 2023-03-16
      • 2019-02-01
      相关资源
      最近更新 更多