Timer 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 Timer 类的新实例。
重载
| 名称 | 说明 |
|---|---|
| Timer(TimerCallback) | |
| Timer(TimerCallback, Object, Int32, Int32) |
使用 32 位有符号整数来指定时间间隔,初始化类的新实例 |
| Timer(TimerCallback, Object, Int64, Int64) |
使用 64 位带符号整数来测量时间间隔,初始化类的新实例 |
| Timer(TimerCallback, Object, TimeSpan, TimeSpan) |
使用 |
| Timer(TimerCallback, Object, UInt32, UInt32) |
使用 32 位无符号整数来测量时间间隔,初始化类的新实例 |
Timer(TimerCallback)
- Source:
- Timer.cs
- Source:
- Timer.cs
- Source:
- Timer.cs
- Source:
- Timer.cs
- Source:
- Timer.cs
public:
Timer(System::Threading::TimerCallback ^ callback);
public Timer(System.Threading.TimerCallback callback);
new System.Threading.Timer : System.Threading.TimerCallback -> System.Threading.Timer
Public Sub New (callback As TimerCallback)
参数
- callback
- TimerCallback
表示 TimerCallback 要执行的方法的委托。
示例
下面的代码示例使用计时器本身作为状态对象创建新的计时器。 该方法 Change 用于启动计时器。 计时器回调发生时,状态对象用于关闭计时器。
using System;
using System.Threading;
public class Example
{
public static void Main()
{
// Create an instance of the Example class, and start two
// timers.
Example ex = new Example();
ex.StartTimer(2000);
ex.StartTimer(1000);
Console.WriteLine("Press Enter to end the program.");
Console.ReadLine();
}
public void StartTimer(int dueTime)
{
Timer t = new Timer(new TimerCallback(TimerProc));
t.Change(dueTime, 0);
}
private void TimerProc(object state)
{
// The state object is the Timer object.
Timer t = (Timer) state;
t.Dispose();
Console.WriteLine("The timer callback executes.");
}
}
Imports System.Threading
Public Class Example
Public Shared Sub Main()
' Create an instance of the Example class, and start two
' timers.
Dim ex As New Example()
ex.StartTimer(2000)
ex.StartTimer(1000)
Console.WriteLine("Press Enter to end the program.")
Console.ReadLine()
End Sub
Public Sub StartTimer(ByVal dueTime As Integer)
Dim t As New Timer(AddressOf TimerProc)
t.Change(dueTime, 0)
End Sub
Private Sub TimerProc(ByVal state As Object)
' The state object is the Timer object.
Dim t As Timer = CType(state, Timer)
t.Dispose()
Console.WriteLine("The timer callback executes.")
End Sub
End Class
注解
如果要将 Timer 对象本身用作状态对象,请调用此构造函数。 创建计时器后,使用 Change 该方法设置间隔和截止日期。
此构造函数指定第一个回调之前的无限截止日期和回调之间的无限间隔,以防止在将对象分配给状态对象之前 Timer 发生第一个回调。
为指定 callback 的方法应重新进入,因为它在线程上 ThreadPool 调用。 如果计时器间隔小于执行该方法所需的时间,或者所有线程池线程都正在使用并且该方法已排队多次,则可以在两个线程池线程上同时执行该方法。
适用于
Timer(TimerCallback, Object, Int32, Int32)
- Source:
- Timer.cs
- Source:
- Timer.cs
- Source:
- Timer.cs
- Source:
- Timer.cs
- Source:
- Timer.cs
使用 32 位有符号整数来指定时间间隔,初始化类的新实例 Timer 。
public:
Timer(System::Threading::TimerCallback ^ callback, System::Object ^ state, int dueTime, int period);
public Timer(System.Threading.TimerCallback callback, object state, int dueTime, int period);
public Timer(System.Threading.TimerCallback callback, object? state, int dueTime, int period);
new System.Threading.Timer : System.Threading.TimerCallback * obj * int * int -> System.Threading.Timer
Public Sub New (callback As TimerCallback, state As Object, dueTime As Integer, period As Integer)
参数
- callback
- TimerCallback
表示 TimerCallback 要执行的方法的委托。
- state
- Object
一个对象,包含回调方法要使用的信息,或 null。
例外
dueTime或period参数为负数,不等于 Infinite。
参数 callback 为 null.
示例
下面的代码示例演示如何创建 TimerCallback 委托并初始化类的新实例 Timer 。
using System;
using System.Threading;
class TimerExample
{
static void Main()
{
// Create an AutoResetEvent to signal the timeout threshold in the
// timer callback has been reached.
var autoEvent = new AutoResetEvent(false);
var statusChecker = new StatusChecker(10);
// Create a timer that invokes CheckStatus after one second,
// and every 1/4 second thereafter.
Console.WriteLine("{0:h:mm:ss.fff} Creating timer.\n",
DateTime.Now);
var stateTimer = new Timer(statusChecker.CheckStatus,
autoEvent, 1000, 250);
// When autoEvent signals, change the period to every half second.
autoEvent.WaitOne();
stateTimer.Change(0, 500);
Console.WriteLine("\nChanging period to .5 seconds.\n");
// When autoEvent signals the second time, dispose of the timer.
autoEvent.WaitOne();
stateTimer.Dispose();
Console.WriteLine("\nDestroying timer.");
}
}
class StatusChecker
{
private int invokeCount;
private int maxCount;
public StatusChecker(int count)
{
invokeCount = 0;
maxCount = count;
}
// This method is called by the timer delegate.
public void CheckStatus(Object stateInfo)
{
AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
Console.WriteLine("{0} Checking status {1,2}.",
DateTime.Now.ToString("h:mm:ss.fff"),
(++invokeCount).ToString());
if(invokeCount == maxCount)
{
// Reset the counter and signal the waiting thread.
invokeCount = 0;
autoEvent.Set();
}
}
}
// The example displays output like the following:
// 11:59:54.202 Creating timer.
//
// 11:59:55.217 Checking status 1.
// 11:59:55.466 Checking status 2.
// 11:59:55.716 Checking status 3.
// 11:59:55.968 Checking status 4.
// 11:59:56.218 Checking status 5.
// 11:59:56.470 Checking status 6.
// 11:59:56.722 Checking status 7.
// 11:59:56.972 Checking status 8.
// 11:59:57.223 Checking status 9.
// 11:59:57.473 Checking status 10.
//
// Changing period to .5 seconds.
//
// 11:59:57.474 Checking status 1.
// 11:59:57.976 Checking status 2.
// 11:59:58.476 Checking status 3.
// 11:59:58.977 Checking status 4.
// 11:59:59.477 Checking status 5.
// 11:59:59.977 Checking status 6.
// 12:00:00.478 Checking status 7.
// 12:00:00.980 Checking status 8.
// 12:00:01.481 Checking status 9.
// 12:00:01.981 Checking status 10.
//
// Destroying timer.
Imports System.Threading
Public Module Example
Public Sub Main()
' Use an AutoResetEvent to signal the timeout threshold in the
' timer callback has been reached.
Dim autoEvent As New AutoResetEvent(False)
Dim statusChecker As New StatusChecker(10)
' Create a timer that invokes CheckStatus after one second,
' and every 1/4 second thereafter.
Console.WriteLine("{0:h:mm:ss.fff} Creating timer." & vbCrLf,
DateTime.Now)
Dim stateTimer As New Timer(AddressOf statusChecker.CheckStatus,
autoEvent, 1000, 250)
' When autoEvent signals, change the period to every half second.
autoEvent.WaitOne()
stateTimer.Change(0, 500)
Console.WriteLine(vbCrLf & "Changing period to .5 seconds." & vbCrLf)
' When autoEvent signals the second time, dispose of the timer.
autoEvent.WaitOne()
stateTimer.Dispose()
Console.WriteLine(vbCrLf & "Destroying timer.")
End Sub
End Module
Public Class StatusChecker
Dim invokeCount, maxCount As Integer
Sub New(count As Integer)
invokeCount = 0
maxCount = count
End Sub
' The timer callback method.
Sub CheckStatus(stateInfo As Object)
Dim autoEvent As AutoResetEvent = DirectCast(stateInfo, AutoResetEvent)
invokeCount += 1
Console.WriteLine("{0:h:mm:ss.fff} Checking status {1,2}.",
DateTime.Now, invokeCount)
If invokeCount = maxCount Then
' Reset the counter and signal the waiting thread.
invokeCount = 0
autoEvent.Set()
End If
End Sub
End Class
' The example displays output like the following:
' 11:59:54.202 Creating timer.
'
' 11:59:55.217 Checking status 1.
' 11:59:55.466 Checking status 2.
' 11:59:55.716 Checking status 3.
' 11:59:55.968 Checking status 4.
' 11:59:56.218 Checking status 5.
' 11:59:56.470 Checking status 6.
' 11:59:56.722 Checking status 7.
' 11:59:56.972 Checking status 8.
' 11:59:57.223 Checking status 9.
' 11:59:57.473 Checking status 10.
'
' Changing period to .5 seconds.
'
' 11:59:57.474 Checking status 1.
' 11:59:57.976 Checking status 2.
' 11:59:58.476 Checking status 3.
' 11:59:58.977 Checking status 4.
' 11:59:59.477 Checking status 5.
' 11:59:59.977 Checking status 6.
' 12:00:00.478 Checking status 7.
' 12:00:00.980 Checking status 8.
' 12:00:01.481 Checking status 9.
' 12:00:01.981 Checking status 10.
'
' Destroying timer.
注解
参数指定的 callback 委托在经过后 dueTime 调用一次,之后每次 period 时间间隔都用完。
如果 dueTime 为零(0), callback 则立即调用。
dueTime如果未Timeout.Infinite调用,callback则禁用计时器,但可以通过调用Change该方法重新启用。
由于 Timer 类的分辨率与系统时钟的分辨率相同,Windows 7和Windows 8系统上大约为 15 毫秒,因此如果 callback 小于系统时钟的分辨率,则 period 委托按系统时钟分辨率定义的时间间隔执行。 如果period为零(0)或Timeout.InfinitedueTime不是Timeout.Infinite,callback则调用一次;计时器的定期行为被禁用,但可以使用该方法重新启用Change。
注释
使用的系统时钟与 GetTickCount 使用的时钟相同,这不受 timeBeginPeriod 和 timeEndPeriod 所做的更改的影响。
为指定 callback 的方法应重新进入,因为它在线程上 ThreadPool 调用。 如果计时器间隔小于执行该方法所需的时间,或者所有线程池线程都正在使用并且该方法已排队多次,则可以在两个线程池线程上同时执行该方法。
另请参阅
适用于
Timer(TimerCallback, Object, Int64, Int64)
- Source:
- Timer.cs
- Source:
- Timer.cs
- Source:
- Timer.cs
- Source:
- Timer.cs
- Source:
- Timer.cs
使用 64 位带符号整数来测量时间间隔,初始化类的新实例 Timer 。
public:
Timer(System::Threading::TimerCallback ^ callback, System::Object ^ state, long dueTime, long period);
public Timer(System.Threading.TimerCallback callback, object? state, long dueTime, long period);
public Timer(System.Threading.TimerCallback callback, object state, long dueTime, long period);
new System.Threading.Timer : System.Threading.TimerCallback * obj * int64 * int64 -> System.Threading.Timer
Public Sub New (callback As TimerCallback, state As Object, dueTime As Long, period As Long)
参数
- callback
- TimerCallback
表示 TimerCallback 要执行的方法的委托。
- state
- Object
一个对象,包含回调方法要使用的信息,或 null。
例外
dueTime或period参数为负数,不等于 Infinite。
dueTime或period参数大于4294967294。
注解
参数指定的 callback 委托在经过后 dueTime 调用一次,之后每次 period 时间间隔都用完。
如果 dueTime 为零(0), callback 则立即调用。
dueTime如果未Timeout.Infinite调用,callback则禁用计时器,但可以通过调用Change该方法重新启用。
由于 Timer 类的分辨率与系统时钟的分辨率相同,Windows 7和Windows 8系统上大约为 15 毫秒,因此如果 callback 小于系统时钟的分辨率,则 period 委托按系统时钟分辨率定义的时间间隔执行。
如果period为零(0)或Timeout.InfinitedueTime不是Timeout.Infinite,callback则调用一次;计时器的定期行为被禁用,但可以使用该方法重新启用Change。
注释
使用的系统时钟与 GetTickCount 使用的时钟相同,这不受 timeBeginPeriod 和 timeEndPeriod 所做的更改的影响。
为指定 callback 的方法应重新进入,因为它在线程上 ThreadPool 调用。 如果计时器间隔小于执行该方法所需的时间,或者所有线程池线程都正在使用并且该方法已排队多次,则可以在两个线程池线程上同时执行该方法。
另请参阅
适用于
Timer(TimerCallback, Object, TimeSpan, TimeSpan)
- Source:
- Timer.cs
- Source:
- Timer.cs
- Source:
- Timer.cs
- Source:
- Timer.cs
- Source:
- Timer.cs
使用Timer值测量时间间隔初始化类的新实例TimeSpan。
public:
Timer(System::Threading::TimerCallback ^ callback, System::Object ^ state, TimeSpan dueTime, TimeSpan period);
public Timer(System.Threading.TimerCallback callback, object state, TimeSpan dueTime, TimeSpan period);
public Timer(System.Threading.TimerCallback callback, object? state, TimeSpan dueTime, TimeSpan period);
new System.Threading.Timer : System.Threading.TimerCallback * obj * TimeSpan * TimeSpan -> System.Threading.Timer
Public Sub New (callback As TimerCallback, state As Object, dueTime As TimeSpan, period As TimeSpan)
参数
- callback
- TimerCallback
表示要执行的方法的委托。
- state
- Object
一个对象,包含回调方法要使用的信息,或 null。
- dueTime
- TimeSpan
调用之前 callback 延迟的时间量。 指定 InfiniteTimeSpan 以防止计时器启动。 指定 Zero 以立即启动计时器。
- period
- TimeSpan
调用 callback的时间间隔 。 指定 InfiniteTimeSpan 以禁用定期信号。
例外
或为负值(不等于dueTime)或大于 period 的毫秒Infinite数。
参数 callback 为 null.
示例
下面的代码示例演示如何创建 TimerCallback 委托并初始化类的新实例 Timer 。
using System;
using System.Threading;
class TimerExample
{
static void Main()
{
AutoResetEvent autoEvent = new AutoResetEvent(false);
StatusChecker statusChecker = new StatusChecker(10);
// Create the delegate that invokes methods for the timer.
TimerCallback timerDelegate =
new TimerCallback(statusChecker.CheckStatus);
TimeSpan delayTime = new TimeSpan(0, 0, 1);
TimeSpan intervalTime = new TimeSpan(0, 0, 0, 0, 250);
// Create a timer that signals the delegate to invoke
// CheckStatus after one second, and every 1/4 second
// thereafter.
Console.WriteLine("{0} Creating timer.\n",
DateTime.Now.ToString("h:mm:ss.fff"));
Timer stateTimer = new Timer(
timerDelegate, autoEvent, delayTime, intervalTime);
// When autoEvent signals, change the period to every
// 1/2 second.
autoEvent.WaitOne(5000, false);
stateTimer.Change(new TimeSpan(0),
intervalTime + intervalTime);
Console.WriteLine("\nChanging period.\n");
// When autoEvent signals the second time, dispose of
// the timer.
autoEvent.WaitOne(5000, false);
stateTimer.Dispose();
Console.WriteLine("\nDestroying timer.");
}
}
class StatusChecker
{
int invokeCount, maxCount;
public StatusChecker(int count)
{
invokeCount = 0;
maxCount = count;
}
// This method is called by the timer delegate.
public void CheckStatus(Object stateInfo)
{
AutoResetEvent autoEvent = (AutoResetEvent)stateInfo;
Console.WriteLine("{0} Checking status {1,2}.",
DateTime.Now.ToString("h:mm:ss.fff"),
(++invokeCount).ToString());
if(invokeCount == maxCount)
{
// Reset the counter and signal Main.
invokeCount = 0;
autoEvent.Set();
}
}
}
Imports System.Threading
Public Class TimerExample
<MTAThread> _
Shared Sub Main()
Dim autoEvent As New AutoResetEvent(False)
Dim statusChecker As New StatusChecker(10)
' Create the delegate that invokes methods for the timer.
Dim timerDelegate As TimerCallback = _
AddressOf statusChecker.CheckStatus
Dim delayTime As New TimeSpan(0, 0, 1)
Dim intervalTime As New TimeSpan(0, 0, 0, 0, 250)
' Create a timer that signals the delegate to invoke
' CheckStatus after one second, and every 1/4 second
' thereafter.
Console.WriteLine("{0} Creating timer." & vbCrLf, _
DateTime.Now.ToString("h:mm:ss.fff"))
Dim stateTimer As Timer = New Timer( _
timerDelegate, autoEvent, delayTime, intervalTime)
' When autoEvent signals, change the period to every
' 1/2 second.
autoEvent.WaitOne(5000, False)
stateTimer.Change( _
new TimeSpan(0), intervalTime.Add(intervalTime))
Console.WriteLine(vbCrLf & "Changing period." & vbCrLf)
' When autoEvent signals the second time, dispose of
' the timer.
autoEvent.WaitOne(5000, False)
stateTimer.Dispose()
Console.WriteLine(vbCrLf & "Destroying timer.")
End Sub
End Class
Public Class StatusChecker
Dim invokeCount, maxCount As Integer
Sub New(count As Integer)
invokeCount = 0
maxCount = count
End Sub
' This method is called by the timer delegate.
Sub CheckStatus(stateInfo As Object)
Dim autoEvent As AutoResetEvent = _
DirectCast(stateInfo, AutoResetEvent)
invokeCount += 1
Console.WriteLine("{0} Checking status {1,2}.", _
DateTime.Now.ToString("h:mm:ss.fff"), _
invokeCount.ToString())
If invokeCount = maxCount Then
' Reset the counter and signal to stop the timer.
invokeCount = 0
autoEvent.Set()
End If
End Sub
End Class
注解
参数指定的 callback 委托在经过后 dueTime 调用一次,之后每次 period 时间间隔都用完。
如果 dueTime 为零(0), callback 则立即调用。 如果 dueTime 为负数(-1)毫秒, callback 则不调用;计时器被禁用,但可以通过调用 Change 该方法重新启用。
由于 Timer 类的分辨率与系统时钟的分辨率相同,Windows 7和Windows 8系统上大约为 15 毫秒,因此如果 callback 小于系统时钟的分辨率,则 period 委托按系统时钟分辨率定义的时间间隔执行。 如果 period 为零(0)或负 1(-1)毫秒且 dueTime 为正, callback 则调用一次;禁用计时器的定期行为,但可以使用该方法 Change 重新启用。
注释
使用的系统时钟与 GetTickCount 使用的时钟相同,这不受 timeBeginPeriod 和 timeEndPeriod 所做的更改的影响。
为指定 callback 的方法应重新进入,因为它在线程上 ThreadPool 调用。 如果计时器间隔小于执行该方法所需的时间,或者所有线程池线程都正在使用并且该方法已排队多次,则可以在两个线程池线程上同时执行该方法。
另请参阅
适用于
Timer(TimerCallback, Object, UInt32, UInt32)
- Source:
- Timer.cs
- Source:
- Timer.cs
- Source:
- Timer.cs
- Source:
- Timer.cs
- Source:
- Timer.cs
重要
此 API 不符合 CLS。
使用 32 位无符号整数来测量时间间隔,初始化类的新实例 Timer 。
public:
Timer(System::Threading::TimerCallback ^ callback, System::Object ^ state, System::UInt32 dueTime, System::UInt32 period);
[System.CLSCompliant(false)]
public Timer(System.Threading.TimerCallback callback, object? state, uint dueTime, uint period);
[System.CLSCompliant(false)]
public Timer(System.Threading.TimerCallback callback, object state, uint dueTime, uint period);
[<System.CLSCompliant(false)>]
new System.Threading.Timer : System.Threading.TimerCallback * obj * uint32 * uint32 -> System.Threading.Timer
Public Sub New (callback As TimerCallback, state As Object, dueTime As UInteger, period As UInteger)
参数
- callback
- TimerCallback
表示要执行的方法的委托。
- state
- Object
一个对象,包含回调方法要使用的信息,或 null。
- 属性
例外
dueTime或period参数为负数,不等于 Infinite。
参数 callback 为 null.
注解
参数指定的 callback 委托在经过后 dueTime 调用一次,之后每次 period 时间间隔都用完。
如果 dueTime 为零(0), callback 则立即调用。
dueTime如果未Timeout.Infinite调用,callback则禁用计时器,但可以通过调用Change该方法重新启用。
由于 Timer 类的分辨率与系统时钟的分辨率相同,Windows 7和Windows 8系统上大约为 15 毫秒,因此如果 callback 小于系统时钟的分辨率,则 period 委托按系统时钟分辨率定义的时间间隔执行。 如果period为零(0)或Timeout.InfinitedueTime不是Timeout.Infinite,callback则调用一次;计时器的定期行为被禁用,但可以使用该方法重新启用Change。
注释
使用的系统时钟与 GetTickCount 使用的时钟相同,这不受 timeBeginPeriod 和 timeEndPeriod 所做的更改的影响。
为指定 callback 的方法应重新进入,因为它在线程上 ThreadPool 调用。 如果计时器间隔小于执行该方法所需的时间,或者所有线程池线程都正在使用并且该方法已排队多次,则可以在两个线程池线程上同时执行该方法。