C#多线程——Timer
来源:互联网 发布:网络节点半径 编辑:程序博客网 时间:2024/06/07 20:38
每隔一定的时间,触发去做指定的事情,可以用timer类。
Timer timer = new Timer(timerDelegate, ParamObject, 1000, 1000) 其中,
1、第一个参数是要执行的方法
2、第二个参数是回调方法要使用的对象信息;
3、第三个参数是延时启动的时间间隔,“0”表示立即启动;
4、第四个参数是Timer的Interval。
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading;
- using System.Collections;
- namespace Demo
- {
- class ParamObject
- {
- public int iCounter = 0;
- public Timer aTimer;
- }
- class Program
- {
- public static void Main(string[] args)
- {
- ParamObject aParamObject = new ParamObject();
- Timer aTimer = new Timer(new TimerCallback(CheckStatus), aParamObject, 1000, 1000);
- aParamObject.aTimer = aTimer;
- while (aParamObject.aTimer != null)
- {
- Thread.Sleep(0);
- }
- Console.ReadLine();
- }
- static void CheckStatus(object paramObject)
- {
- ParamObject aParamObject = (ParamObject)paramObject;
- aParamObject.iCounter++;
- Console.WriteLine("{0} status: {1}", DateTime.Now.TimeOfDay, aParamObject.iCounter);
- if (aParamObject.iCounter == 5)
- {
- // Wait 1000 ms and go on to do the timer event
- aParamObject.aTimer.Change(1000, 2000);
- Console.WriteLine("Timer interval changed!");
- }
- if (aParamObject.iCounter == 10)
- {
- Console.WriteLine("Dispose timer");
- aParamObject.aTimer.Dispose();
- aParamObject.aTimer = null;
- }
- }
- }
- }
0 0
- C#多线程——Timer
- C#多线程——Timer
- C#多线程(四)——Timer的使用
- C#多线程编程介绍——使用thread、threadpool、timer
- C#多线程编程介绍——使用thread、threadpool、timer
- 【多线程】——定时器Timer
- C#构建多线程应用程序(5) —— 使用System.Threading.Timer
- Java_基础—多线程(Timer计时器)
- C#中不同类timer实现多线程
- C# 在多线程中调用Timer
- 细细品味C#——Timer及多线程编程
- Matlab——Timer对象(多线程编程)
- C# WinForm多线程(二)---- ThreadPool 与 Timer
- C# WinForm多线程开发(二) ThreadPool 与 Timer
- c# System.Threading.Timer是多线程,并能同时执行
- (c# ) timer
- C# Timer
- C# Timer
- CSS中的绝对定位与相对定位
- 13.4 I/O缓冲小结
- ubuntu安装原生迅雷,让下载成为简单
- 九种方法
- Android判断是否挂载外置sd卡
- C#多线程——Timer
- ubuntu下的软件管理
- 推送
- DedeCMS group.php SQL注入漏洞
- ruby中Class的allocate
- 13.5 就I/O模式向内核提出建议
- SpringMVC注解映射
- POJ 2377 Bad Cowtractors【最小生成树变形&&最大生成树】
- 淘宝内部分享:怎么跳出MySQL的10个大坑(上)