TimerCount.cs
1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*-------------------------------------------------------------------------------------------
* 用来测试程序速度的类
* 创建:杨斌 2011-11-19
* ----------------------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows.Forms;
namespace GeneralLib
{
/// <summary>
/// 用来测试程序速度的类
/// </summary>
class TimerCount
{
/// <summary>
/// 上次记录的毫秒数
/// </summary>
private int TickCountBegin = 0;
[DllImport("winmm")]
static extern int timeGetTime();
/// <summary>
/// 获取系统启动后经过的毫秒数
/// </summary>
/// <returns></returns>
public static int TickCount()
{
return Environment.TickCount;
//return timeGetTime();
}
/// <summary>
/// 开始计时,毫秒
/// </summary>
public void StartCount()
{
TickCountBegin = TickCount();
}
/// <summary>
/// 获取开始计时后经过的时间,毫秒
/// </summary>
/// <returns></returns>
public int GetCountMS()
{
return TickCount() - TickCountBegin;
}
public static void Delay(long ms)
{
Stopwatch t = Stopwatch.StartNew();
while (t.ElapsedMilliseconds < ms)
{
Application.DoEvents();
}
}
}
}