Timer控件使用(System.Timers.Timer)
2015-08-22 09:29
369 查看
System.Timers.Timer t = new System.Timers.Timer(10000);
//实例化Timer类,设置间隔时间为10000毫秒;
t.Elapsed += new System.Timers.ElapsedEventHandler(theout);
//到达时间的时候执行事件;
t.AutoReset = true;
//设置是执行一次(false)还是一直执行(true);
t.Enabled = true;
//需要调用 timer.Start()或者timer.Enabled = true来启动它, timer.Start()的内部原理还是设置timer.Enabled = true;
//调用 timer.Stop()或者timer.Enabled = false来停止引发Elapsed事件, timer.Stop()的内部原理还是设置timer.Enabled = false,最重要的是timer.Enabled = false后会取消线程池中当前等待队列中剩余任务的执行。
举例:
class timerExample
{
int num = 0;
//IUSObject blinkObj=null;
System.Timers.Timer t = null;
public static void Init()
{
//blinkObj = new IUSObject();
t = new System.Timers.Timer(1000);
t.Elapsed += new System.Timers.ElapsedEventHandler(willDo);
t.AutoReset = true;
t.Enabled = true;
}
public static void willDo(object sender, System.Timers.ElapsedEventArgs e)
{
//blinkObj.Visible = blinkObj.Visible == true ? false : true;
MessageBox.Show("每隔1000毫秒执行一次");
if(num>=6)
{
System.Timers.Timer t = (System.Timers.Timer)sender;
t.Enable = false;
}
num += 1;
}
}
说明:若要指定timer的循环次数,需通过num辅助 执行,多次执行Init时,需先将timer的elapsed事件停止(即隐去的内容),然后再次绑定
//实例化Timer类,设置间隔时间为10000毫秒;
t.Elapsed += new System.Timers.ElapsedEventHandler(theout);
//到达时间的时候执行事件;
t.AutoReset = true;
//设置是执行一次(false)还是一直执行(true);
t.Enabled = true;
//需要调用 timer.Start()或者timer.Enabled = true来启动它, timer.Start()的内部原理还是设置timer.Enabled = true;
//调用 timer.Stop()或者timer.Enabled = false来停止引发Elapsed事件, timer.Stop()的内部原理还是设置timer.Enabled = false,最重要的是timer.Enabled = false后会取消线程池中当前等待队列中剩余任务的执行。
举例:
class timerExample
{
int num = 0;
//IUSObject blinkObj=null;
System.Timers.Timer t = null;
public static void Init()
{
//blinkObj = new IUSObject();
t = new System.Timers.Timer(1000);
t.Elapsed += new System.Timers.ElapsedEventHandler(willDo);
t.AutoReset = true;
t.Enabled = true;
}
public static void willDo(object sender, System.Timers.ElapsedEventArgs e)
{
//blinkObj.Visible = blinkObj.Visible == true ? false : true;
MessageBox.Show("每隔1000毫秒执行一次");
if(num>=6)
{
System.Timers.Timer t = (System.Timers.Timer)sender;
t.Enable = false;
}
num += 1;
}
}
说明:若要指定timer的循环次数,需通过num辅助 执行,多次执行Init时,需先将timer的elapsed事件停止(即隐去的内容),然后再次绑定
相关文章推荐
- JUnit4中的测试套件
- JAVA代理模式
- hdu-5384Danganronpa+多校训练+AC自动机
- 快速排序
- 用JUnit4进行参数化测试
- soapui的安装
- 优秀项目经理应备技能04-项目管理的原则
- 不要上传销的当
- PostgreSQL Replication之第八章 与pgbouncer一起工作(4)
- log4j
- web项目中的各种路径的获取
- C++ 读取键盘输入(cin/cin.getline()/cin.get()/cin.clear())
- C和C++结构体的初始化和赋值
- 为自己的网站或者是项目添加追加日志功能
- 打开远程桌面连接的快捷方式
- logstash时差问题
- 图论08——次短路的距离及路径
- 第二章 2-1 显示图像
- Test注解的两个属性:expected和timeout
- JAVA中内存溢出和内存泄露