timer控件的常用属性
1:Timer.Enabled 属性用于设置是否启用定时器
2:Timer.Interval 属性,事件的间隔,单位毫秒
3:Timer.Elapsed 事件,达到间隔时发生
(1 ) window中
方法1:在visual studio工具箱中加入Timer控件,点击timer控件进入事件:
private void timer1_Tick(object sender, EventArgs e)
{
//在这加入自己想要的代码
}
方法2:直接用代码,而不是控件:
using System.Timers; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { System.Timers.Timer aTimer = new System.Timers.Timer(); aTimer.Elapsed += new ElapsedEventHandler(OnTimedShow); aTimer.Interval = 1000; aTimer.Enabled = true; //timer可用 } private static void OnTimedShow(object source, ElapsedEventArgs e) { Console.WriteLine("1秒钟后显示"); } } }
(2)ASP.NET AJAX中的服务端Timer控件 1.添加新页面并切换到设计视图。 2.如果页面没有包含ScriptManager控件,在工具箱的AJAX Extensions标签下双击ScriptManager控件添加到页面中。 3.单击ScriptManager控件并双击UpdatePanel控件添加到页面中。 4.在UpdatePanel控件内单击并双击Timer控件添加到UpdatePanel中。Timer控件可以作为UpdatePanel的触发器不管是否在UpdatePanel中。 5.在UpdatePanel控件中添加一个Label控件。 然后点击timer控件进入事件,方法和上面一样: timer1.Interval=1000;
protected void Timer1_Tick(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToLongTimeString();
}
会发现只有UpdatePanel中刷新,页面中其它的地方不变