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
查看全文
相关阅读:
钱多多软件制作04
团队项目01应用场景
HDU 4411 arrest
HDU 4406 GPA
HDU 3315 My Brute
HDU 3667 Transportation
HDU 2676 Matrix
欧拉回路三水题 POJ 1041 POJ 2230 POJ 1386
SPOJ 371 BOXES
POJ 3422 Kaka's Matrix Travels
原文地址:https://www.cnblogs.com/mishy/p/1453210.html
最新文章
[hiho1159] Poker
[bzoj3174] 拯救小矮人
[bzoj4974] 字符串大师
[hiho1160] 攻城略地
[poj3723] Conscription
剑指offer-第四章解决面试题思路(字符串的排序)
剑指offer-第四章解决面试题思路(二叉收索树和双向链表)
剑指offer-第四章解决面试题思路(复杂链表的复制)
简直offer-第四章解决面试题思路(二叉树中和为某一值的路径)
剑指offer-第四章解决面试题思路(判断一个数组是否为二叉搜索树的后序遍历序列)
热门文章
Intent详解以及实例
Fragment详解及举例
Android DVM
Android开源框架-Annotation框架(以ViewMapping注解为例)
钱多多软件制作08
构建之法阅读笔记05
钱多多软件制作07
构建之法阅读笔记04
钱多多软件制作06
钱多多软件制作05
Copyright © 2011-2022 走看看