Environment.TickCount 属性

定义

获取系统启动后经过的毫秒数。

public:
 static property int TickCount { int get(); };
public static int TickCount { get; }
static member TickCount : int
Public Shared ReadOnly Property TickCount As Integer

属性值

一个 32 位带符号整数,它包含自上次启动计算机以来所经过的时间(以毫秒为单位)。

示例

以下示例演示如何检索 属性返回 TickCount 的值的正范围。 属性TickCount在 (为负数)和 Int32.MaxValue 49.8 天之间Int32.MinValue循环一次。 此代码示例删除符号位以生成一个非否定数,该数字在零到 MaxValue 每 24.9 天周期一次之间。

// Sample for the Environment::TickCount property
// TickCount cycles between Int32::MinValue, which is a negative 
// number, and Int32::MaxValue once every 49.8 days. This sample
// removes the sign bit to yield a nonnegative number that cycles 
// between zero and Int32::MaxValue once every 24.9 days.
using namespace System;
int main()
{
   int result = Environment::TickCount & Int32::MaxValue;
   Console::WriteLine( "TickCount: {0}", result );
}

/*
This example produces an output similar to the following:

TickCount: 101931139
*/
// Sample for the Environment.TickCount property.

// TickCount cycles between Int32.MinValue, which is a negative
// number, and Int32.MaxValue once every 49.8 days. This sample
// removes the sign bit to yield a nonnegative number that cycles
// between zero and Int32.MaxValue once every 24.9 days.

using System;

class Sample
{
    public static void Main()
    {
    int result = Environment.TickCount & Int32.MaxValue;
    Console.WriteLine("TickCount: {0}", result);
    }
}
/*
This example produces an output similar to the following:

TickCount: 101931139
*/
// Sample for the Environment.TickCount property.

// TickCount cycles between Int32.MinValue, which is a negative
// number, and Int32.MaxValue once every 49.8 days. This sample
// removes the sign bit to yield a nonnegative number that cycles
// between zero and Int32.MaxValue once every 24.9 days.

open System

let result = Environment.TickCount &&& Int32.MaxValue
printfn $"TickCount: {result}"

// This example produces an output similar to the following:
//     TickCount: 101931139

' Sample for the Environment.TickCount property.
' TickCount cycles between Int32.MinValue, which is a negative 
' number, and Int32.MaxValue once every 49.8 days. This sample
' removes the sign bit to yield a nonnegative number that cycles 
' between zero and Int32.MaxValue once every 24.9 days.

Class Sample
   Public Shared Sub Main()
      Dim result As Integer = Environment.TickCount And Int32.MaxValue

      Console.WriteLine("TickCount: {0}", result)
   End Sub
End Class
'
'This example produces an output similar to the following:
'
'TickCount: 101931139
'

注解

此属性的值派生自系统计时器,并存储为 32 位有符号整数。 请注意,由于它派生自系统计时器,因此 属性的 TickCount 分辨率仅限于系统计时器的分辨率,通常介于 10 到 16 毫秒之间。

重要

由于属性值的值 TickCount 是一个 32 位有符号整数,如果系统连续运行, TickCount 则会在大约 24.9 天内从零递增到 Int32.MaxValue ,然后跳转到 Int32.MinValue负数,然后在接下来的 24.9 天内递增回零。 可以通过调用 Windows GetTickCount 函数(大约 49.7 天后重置为零)或通过调用 GetTickCount64 函数来解决此问题。

TickCount 不同于 DateTime.Ticks 属性,该属性是自 1/1/0001 上午 12:00 以来经过的 100 纳秒间隔数。

DateTime.Now使用 属性获取此计算机上的当前本地日期和时间。

适用于