日期:2014-05-19  浏览次数:20874 次

定时器timer的问题,求教
我在WEB程序中这样用,为什么页面上死活不出字啊,高手指教


  protected   void   Page_Load(object   sender,   EventArgs   e)
        {
                  System.Timers.Timer   timer1   =   new   System.Timers.Timer();
                timer1.Elapsed   +=   new   System.Timers.ElapsedEventHandler(this.timer1_Tick);
                timer1.Interval   =   2000;
                timer1.Start();
             
               

        }
     
        private   void   timer1_Tick(object   sender,   ElapsedEventArgs   e)
        {
             
                        Response.Write( "good ");
               
        }

------解决方案--------------------
当然不能用了,想想运行的机制就知道了,服务器处理完以后就把内容发送到客户端了,不会再保存内容了,这个时候永远也不会执行这个处理的,所以你应该用客户端的定时器了啊,setinterval
------解决方案--------------------
楼上正解
------解决方案--------------------
ASP.Net事物状态连接,而且声名定时器应该用个委托来代理你要执行的函数。而且在这里面定时器是另一个线程,跨线呈调用这样可能会出问题。

using System.Threading;
namespace 定时器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int i;
System.Threading.Timer timer1;
public delegate void del_text(TextBox tb);
public void aa(object ab)
{
i++;
Invoke(new del_text(display), new object[] {textBox1});
}
public void display(TextBox tex)
{
textBox1 = tex;
tex.Text = i.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
//使用定时器的委托,用来代理在定时器重要执行的方法,此方法需要有一个object类型的形参
TimerCallback del = new TimerCallback(aa);
//初始化定时器,重载参数按提示的走
timer1 = new System.Threading.Timer(del, null, 0, 1000);
}
------解决方案--------------------
Web形式的定时器timer只能在服务器上起作用的;

如果你想要在客户端页面上面表现出来;可以用javascript试试
比如说显示即时:
function clock()
{
//本函数需要在 <body MS_POSITIONING= "GridLayout "> 中加入onload= "clock() "
var timeStr;
now = new Date();
var years = now.getFullYear();//年
var months = now.getMonth()+1;//月
var days = now.getDate();
var hours=now.getHours();
var minutes=now.getMinutes();
var seconds=now.getSeconds();
timeStr= "现在时刻: "+years+ " 年 "+months+ " 月 "+days+ " 日 "+hours+ " 时 "+minutes+ " 分 "+seconds+ " 秒 ";
clock1.innerHTML=timeStr;//其中clock1是页面中div的控件的ID
window.setTimeout( "clock() ",1000);
}

------解决方案--------------------
必须用脚本更新数据!
------解决方案--------------------
web方式要用定时器,要采用Ajax方式。否则客户端的事件服务器是收不到的。
------解决方案--------------------
用的是2003还是2005,2005里面不允许跨线呈调用控件