using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;namespace WindowsApplication1 { public partial class Form2 : Form { public Form2() { InitializeComponent(); }private void Form2_Load(object sender, EventArgs e) { //设置timer的每隔多长时间执行一次,这里设置为1000毫秒,也就是1秒 this.timer1.Interval = 1000; //启动timer this.timer1.Start(); }//初始时间为30秒 int time = 30;//timer事件 private void timer1_Tick(object sender, EventArgs e) { //一个label显示时间 label1.Text = time + "";time = time - 1; //如果时间结束,关闭窗体! if (time == 0) { this.Close(); } } } }
双击timer控件,在里面写要执行的代码就欧克
private void testtimer() // 下面代码可以不用放在此方法 放在你想要的地方 比如 加载事件 按钮事件 { int time = 30; System.Timers.Timer t; t = new System.Timers.Timer(time * 1000); //实例化Timer类,设置间隔时间为30*1000毫秒即30秒; t.Elapsed += new System.Timers.ElapsedEventHandler(youdo); //到达时间的时候执行事件; t.AutoReset = true; //设置是执行一次(false)还是一直执行(true); } private void youdo(object source, System.Timers.ElapsedEventArgs e) { //30秒你要做的事情在此写 }
Visual Studio 和 .NET Framework 中包含三个计时器控件:始终位于“工具箱”中的基于 Windows 的计时器 ---Windows计时器可添加到“工具箱”中的基于服务器的计时器 ---服务器计时器 可通过编程方式使用的线程计时器 ---线程计时器基于 Windows 的计时器针对在 Windows 窗体应用程序中使用而进行了优化。基于服务器的计时器是传统的计时器为了在服务器环境上运行而优化后的更新版本。线程计时器是一种简单的、轻量级计时器,它使用回调方法而不是使用事件,并由线程池线程提供支持。在 Win32 体系结构中有两种类型的线程:UI 线程和辅助线程。UI 线程绝大多数时间处于空闲状态,等待消息循环中的消息到来。一旦接收到消息,它们就进行处理并等待下一个消息到来。另外,辅助线程用来执行后台处理而且不使用消息循环。Windows 计时器和基于服务器的计时器在运行时都使用 Interval 属性。线程计时器的时间间隔在 Timer 构造函数中设置。计时器的设计目的各不相同,它们的线程处理明确地指出了这一点: Windows 计时器是为单线程环境设计的,其中,UI 线程用于执行处理。Windows 计时器的精度限定为 55 毫秒。这些传统计时器要求用户代码有一个可用的 UI 消息泵,而且总是在同一个线程中操作,或者将调用封送到另一个线程。对于 COM 组件来说,这样会降低性能。 基于服务器的计时器是为在多线程环境下与辅助线程一起使用而设计的。由于它们使用不同的体系结构,因此基于服务器的计时器可能比 Windows 计时器精确得多。服务器计时器可以在线程之间移动来处理引发的事件。 对消息不在线程上发送的方案中,线程计时器是非常有用的。例如,基于 Windows 的计时器依赖于操作系统计时器的支持,如果不在线程上发送消息,与计时器相关的事件将不会发生。在这种情况下,线程计时器就非常有用。
设置一个30秒倒计时,然后在执行你要执行的某事件 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms;namespace sixunhuan { public partial class Form1 : Form { int time = 30; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { timer1.Start(); } private void timer1_Tick(object sender, EventArgs e) { if (time > 0) { time--; label1.Text = Convert.ToString(time); } else { timer1.Stop(); } } } }
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}private void Form2_Load(object sender, EventArgs e)
{
//设置timer的每隔多长时间执行一次,这里设置为1000毫秒,也就是1秒
this.timer1.Interval = 1000;
//启动timer
this.timer1.Start();
}//初始时间为30秒
int time = 30;//timer事件
private void timer1_Tick(object sender, EventArgs e)
{
//一个label显示时间
label1.Text = time + "";time = time - 1;
//如果时间结束,关闭窗体!
if (time == 0)
{
this.Close();
}
}
}
}
private void testtimer() // 下面代码可以不用放在此方法 放在你想要的地方 比如 加载事件 按钮事件
{
int time = 30;
System.Timers.Timer t;
t = new System.Timers.Timer(time * 1000); //实例化Timer类,设置间隔时间为30*1000毫秒即30秒;
t.Elapsed += new System.Timers.ElapsedEventHandler(youdo); //到达时间的时候执行事件;
t.AutoReset = true; //设置是执行一次(false)还是一直执行(true);
}
private void youdo(object source, System.Timers.ElapsedEventArgs e)
{
//30秒你要做的事情在此写
}
点属性,在Interval处设置时间(是毫秒30秒=30000毫秒) 30000
完了 点事件
就一个timer1_Tick
双击进入
private void timer1_Tick(object sender, EventArgs e)
{
这自然就是30秒以后执行的东东//
} 友情提示: timer是会循环的,上面是30秒以后再次执行此操作,如果不需要,
你要判断一下,是否已经执行过你要的东东,如果是,就不用再执行了
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace sixunhuan
{
public partial class Form1 : Form
{
int time = 30;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
} private void timer1_Tick(object sender, EventArgs e)
{
if (time > 0)
{
time--;
label1.Text = Convert.ToString(time);
}
else
{
timer1.Stop();
}
}
}
}