zoukankan
html css js c++ java
VSTS2008 学习之路(2):多线程Timer
今天学习一下多线程在网络上看到一个很好的例子,简单而且很好理解。
Code
1
namespace
ThreadTest
2
{
3
using
System;
4
using
System.Threading;
5
class
TimerExampleState
6
{
7
public
int
counter
=
0
;
8
public
Timer tmr;
9
}
10
class
App
11
{
12
public
static
void
Main()
13
{
14
TimerExampleState s
=
new
TimerExampleState();
15
16
//
创建代理对象TimerCallback,该代理将被定时调用
17
TimerCallback timerDelegate
=
new
TimerCallback(CheckStatus);
18
19
//
创建一个时间间隔为1s的定时器
20
Timer timer
=
new
Timer(timerDelegate, s,
1000
,
1000
);
//
在超过 dueTime 后及此后每隔 period 时间间隔,都会调用一次由 callback 参数指定的委托。
21
s.tmr
=
timer;
22
23
//
主线程停下来等待Timer对象的终止
24
while
(s.tmr
!=
null
)
25
{
26
Thread.Sleep(
0
);
27
}
28
Console.WriteLine(
"
Timer example done.
"
);
29
Console.ReadLine();
30
}
31
//
file:
//
下面是被定时调用的方法
32
33
static
void
CheckStatus(Object state)
34
{
35
TimerExampleState s
=
(TimerExampleState)state;
36
s.counter
++
;
37
Console.WriteLine(
"
{0} Checking Status {1}.
"
, DateTime.Now.TimeOfDay, s.counter);
38
if
(s.counter
==
5
)
39
{
40
file:
//
使用Change方法改变了时间间隔
41
(s.tmr).Change(
10000
,
2000
);
42
Console.WriteLine(
"
changed
"
);
43
}
44
if
(s.counter
==
10
)
45
{
46
Console.WriteLine(
"
disposing of timer
"
);
47
s.tmr.Dispose();
48
s.tmr
=
null
;
49
}
50
}
51
}
52
}
53
查看全文
相关阅读:
[转]Spring的IOC原理[通俗解释一下]
自我介绍
什么是存储过程
Linux 之 最常用的20条命令
[转]sql语句中出现笛卡尔乘积 SQL查询入门篇
mysql 多表连接
正则表达式
postman 测试API
[转]mysql 视图
数据库 修改统一显示时间
原文地址:https://www.cnblogs.com/mishy/p/1453210.html
最新文章
java Socket和ServerSocket多线程编程
HTTP 错误 404.3 -Not Found
plsql 查询 卡死问题解决
vs 调试不进入断点
stdole.dll
OracleDBConsoleorcl 启动不了 服务特定错误2【解决办法】
ORACLE数据库数据文件转移方法(不同于move方法)
c程序的编译
Cookie、sessionStorage、localStorage的区别
Linux常用命令
热门文章
css中bfc和ifc
css左侧固定宽度右侧自适应
div模拟textarea
db2字段修改
mq常用命令
分布式缓存技术redis学习系列(四)——redis高级应用(集群搭建、集群分区原理、集群操作)
分布式缓存技术redis学习系列(三)——redis高级应用(主从、事务与锁、持久化)
Java输入输出流
Java基础算法集50题
java中queue的使用
Copyright © 2011-2022 走看看