Timer 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化 Timer 類別的新執行個體。
多載
| 名稱 | Description |
|---|---|
| Timer(TimerCallback) | |
| Timer(TimerCallback, Object, Int32, Int32) |
初始化該類別的新實例 |
| Timer(TimerCallback, Object, Int64, Int64) |
初始化該類別的新實例 |
| Timer(TimerCallback, Object, TimeSpan, TimeSpan) |
初始化類別的新實例 |
| Timer(TimerCallback, Object, UInt32, UInt32) |
初始化該類別的新實例 |
Timer(TimerCallback)
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)
初始化該類別的新實例 Timer ,使用一個 32 位元有符號整數來指定時間區間。
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);
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)或dueTimeTimeout.Infinite且非Timeout.Infinitecallback,則會呼叫一次;計時器的週期行為會被停用,但可透過此Change方法重新啟用。
Note
所使用的系統時鐘與 GetTickCount 相同,且不受 timeBeginPeriod 與 timeEndPeriod 的變更影響。
指定的方法 callback 應該是重入式,因為它是在執行緒上被呼叫 ThreadPool 的。 如果計時器間隔少於執行方法所需時間,或所有執行緒池執行緒都在使用且方法被多次排隊,則可同時在兩個執行緒池執行緒上執行此方法。
另請參閱
適用於
Timer(TimerCallback, Object, Int64, Int64)
初始化該類別的新實例 Timer ,使用 64 位元有符號整數來測量時間區間。
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);
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)或dueTimeTimeout.Infinite且非Timeout.Infinitecallback,則會呼叫一次;計時器的週期行為會被停用,但可透過此Change方法重新啟用。
Note
所使用的系統時鐘與 GetTickCount 相同,且不受 timeBeginPeriod 與 timeEndPeriod 的變更影響。
指定的方法 callback 應該是重入式,因為它是在執行緒上被呼叫 ThreadPool 的。 如果計時器間隔少於執行方法所需時間,或所有執行緒池執行緒都在使用且方法被多次排隊,則可同時在兩個執行緒池執行緒上執行此方法。
另請參閱
適用於
Timer(TimerCallback, Object, TimeSpan, TimeSpan)
初始化類別的新實例 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);
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中的毫秒數為負,且不等於 Infinite,或大於 Int32.MaxValue。period
參數 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且 dueTime 為正 callback ,則呼叫一次;計時器的週期行為會被禁用,但可使用此 Change 方法重新啟用。
Note
所使用的系統時鐘與 GetTickCount 相同,且不受 timeBeginPeriod 與 timeEndPeriod 的變更影響。
指定的方法 callback 應該是重入式,因為它是在執行緒上被呼叫 ThreadPool 的。 如果計時器間隔少於執行方法所需時間,或所有執行緒池執行緒都在使用且方法被多次排隊,則可同時在兩個執行緒池執行緒上執行此方法。
另請參閱
適用於
Timer(TimerCallback, Object, UInt32, UInt32)
重要
此 API 不符合 CLS 規範。
初始化該類別的新實例 Timer ,使用 32 位元無符號整數來測量時間區間。
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)>]
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)或dueTimeTimeout.Infinite且非Timeout.Infinitecallback,則會呼叫一次;計時器的週期行為會被停用,但可透過此Change方法重新啟用。
Note
所使用的系統時鐘與 GetTickCount 相同,且不受 timeBeginPeriod 與 timeEndPeriod 的變更影響。
指定的方法 callback 應該是重入式,因為它是在執行緒上被呼叫 ThreadPool 的。 如果計時器間隔少於執行方法所需時間,或所有執行緒池執行緒都在使用且方法被多次排隊,則可同時在兩個執行緒池執行緒上執行此方法。