我做了一个小实验,在页面上,放了两个Updatepanel,每个里面分别放了一个label,用来显示当前的时间。又分别用两个timer来控制上面两个updatepanel的刷新频率。一个timer1的刷新频率为1秒,另一个timer2是10秒。timer1的tick事件里,就是更新label1的时间。timer2的tick事件里,更新label1和label2的时间,并且做了一个延迟操作,这么做的意思,就是让timer2的tick操作花费一些时间。。运行问题出现了我的初衷是,两个timer各自单独刷新,不要互相影响。可是,现在,在timer2的tick事件处理时,貌似timer1的tick没有响应。。貌似这timer2在影响timer1?为什么呢?怎样才能让两个timer单独刷新???运行结果是,label2里的时间是每10秒刷新一次,正常。而label1里的时间,5秒内,正常一秒刷新一次,然后5秒内,定在那里不动如下。。
2011-6-10 10:40:18
2011-6-10 10:40:19
2011-6-10 10:40:20
2011-6-10 10:40:21
2011-6-10 10:40:22
2011-6-10 10:40:23
2011-6-10 10:40:28
2011-6-10 10:40:29
2011-6-10 10:40:30
2011-6-10 10:40:31
2011-6-10 10:40:32
2011-6-10 10:40:33
2011-6-10 10:40:38
代码如下。。前台:
<asp:ScriptManager id="scriptManager1" runat="server"></asp:ScriptManager>
    
    <asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="timer1" EventName="Tick" />
    </Triggers>
    <ContentTemplate>
        <asp:Label ID="l1" runat="server"></asp:Label>
        
    </ContentTemplate>
    </asp:UpdatePanel>
    <asp:Timer ID="timer1" runat="server" Interval="1000" ontick="timer1_Tick"></asp:Timer>
     
    <asp:UpdatePanel ID="up2" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="timer2" EventName="Tick" />
    </Triggers>
    <ContentTemplate>
        <asp:Label ID="l2" runat="server"></asp:Label>
        
    </ContentTemplate>
    </asp:UpdatePanel>
    <asp:Timer ID="timer2" runat="server" Interval="10000" ontick="timer2_Tick"></asp:Timer>
后台:
protected void Page_Load(object sender, EventArgs e)
        {
            scriptManager1.RegisterAsyncPostBackControl(timer2);
            l1.Text = DateTime.Now.ToLocalTime().ToString();
            l2.Text = DateTime.Now.ToLocalTime().ToString();
        }        protected void timer1_Tick(object sender, EventArgs e)
        {
            l1.Text = DateTime.Now.ToLocalTime().ToString();
        }        protected void timer2_Tick(object sender, EventArgs e)
        {
            l1.Text = DateTime.Now.ToLocalTime().ToString();
            l2.Text = DateTime.Now.ToLocalTime().ToString();
            for (int i = 0; i < 100000000; i++)
            {
                ;
            }
            for (int i = 0; i < 100000000; i++)
            {
                ;
            }
            for (int i = 0; i < 100000000; i++)
            {
                ;
            }
            for (int i = 0; i < 100000000; i++)
            {
                ;
            }
            for (int i = 0; i < 100000000; i++)
            {
                ;
            }
            for (int i = 0; i < 100000000; i++)
            {
                ;
            }
            for (int i = 0; i < 100000000; i++)
            {
                ;
            }
            for (int i = 0; i < 100000000; i++)
            {
                ;
            }
            for (int i = 0; i < 100000000; i++)
            {
                ;
            }
            for (int i = 0; i < 100000000; i++)
            {
                ;
            }
        }

解决方案 »

  1.   

    记住,timer不是线程,搞清楚
      

  2.   

    就算你放10个Timer控件,运行时在浏览器上也就是这一个页面。这是web的基本机制。当有一个Timer到期,浏览器访问服务器,然后等待服务器返回(对于UpdatePanel来说就是等待返回部分html片段以及其它信息)。这个时间,浏览器“呆”在那里什么也不干,更不是在运行timer。
      

  3.   

    如果需要真正的异步回调,自己使用轻量级的ajax来写异步回调服务器,不要使用Timer控件。这样其实就不是通过页面的Form的Submit提交,也就不会阻塞。但是这应该是访问服务器端最ashx、asmx等等与页面无关的,轻量级的ajax根本不会像UpdatePanel那样去维护控件状态。