Timer.Change 方法

定義

改變計時器的開始時間及方法呼叫間隔。

多載

名稱 Description
Change(Int32, Int32)

改變計時器的開始時間及方法呼叫間隔,使用 32 位元有符號整數來測量時間區間。

Change(Int64, Int64)

改變計時器的開始時間及方法呼叫間隔,使用 64 位元有號整數來衡量時間區間。

Change(TimeSpan, TimeSpan)

改變計時器的開始時間及方法呼叫間隔,並利用 TimeSpan 數值來衡量時間區間。

Change(UInt32, UInt32)

改變計時器的開始時間及方法呼叫間隔,使用 32 位元無符號整數來測量時間區間。

Change(Int32, Int32)

改變計時器的開始時間及方法呼叫間隔,使用 32 位元有符號整數來測量時間區間。

public:
 bool Change(int dueTime, int period);
public bool Change(int dueTime, int period);
member this.Change : int * int -> bool
Public Function Change (dueTime As Integer, period As Integer) As Boolean

參數

dueTime
Int32

呼叫回撥方法前的延遲時間,以毫秒為單位,決定了該方法的建構時間 Timer 。 指定 Infinite 防止計時器重新啟動。 指定零(0)即可立即重新啟動計時器。

period
Int32

回調方法呼叫間隔的時間區間,以毫秒為單位,表示該方法的建構時間 Timer 。 指定 Infinite 關閉週期性訊號。

傳回

true如果計時器成功更新;否則,。 false

例外狀況

這些 Timer 已經被處理掉了。

dueTimeperiod參數為負值,且不等於Infinite

範例

以下程式碼範例示範如何啟動一個 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.

備註

回調方法在經過後 dueTime 會被呼叫一次,之後則在經過時間間隔 period 內呼叫一次。

dueTime 為零(0),則會立即呼叫回調方法。 若 dueTimeTimeout.Infinite則回調方法永遠不會被呼叫;計時器會被停用,但可透過呼叫 Change 並指定正值來 dueTime重新啟用。

period 為零(0)或 Timeout.Infinite,且 dueTimeTimeout.Infinite,則呼叫方法會被呼叫一次;計時器的週期行為會被停用,但可透過呼叫 Change 並指定正值來 period重新啟用。

Change這個方法可以由TimerCallback代理人呼叫。

另請參閱

適用於

Change(Int64, Int64)

改變計時器的開始時間及方法呼叫間隔,使用 64 位元有號整數來衡量時間區間。

public:
 bool Change(long dueTime, long period);
public bool Change(long dueTime, long period);
member this.Change : int64 * int64 -> bool
Public Function Change (dueTime As Long, period As Long) As Boolean

參數

dueTime
Int64

呼叫回撥方法前的延遲時間,以毫秒為單位,決定了該方法的建構時間 Timer 。 指定 Infinite 防止計時器重新啟動。 指定零(0)即可立即重新啟動計時器。 這個值必須小於或等於4294967294。

period
Int64

回調方法呼叫間隔的時間區間,以毫秒為單位,表示該方法的建構時間 Timer 。 指定 Infinite 關閉週期性訊號。

傳回

true如果計時器成功更新;否則,。 false

例外狀況

這些 Timer 已經被處理掉了。

dueTimeperiod 小於 -1。

-或-

dueTimeperiod 大於4294967294。

備註

回調方法在經過後 dueTime 會被呼叫一次,之後則在經過時間間隔 period 內呼叫一次。

dueTime 為零(0),則會立即呼叫回調方法。 若 dueTimeTimeout.Infinite則回調方法永遠不會被呼叫;計時器會被停用,但可透過呼叫 Change 並指定正值來 dueTime重新啟用。

period 為零(0)或 Timeout.Infinite,且 dueTimeTimeout.Infinite,則呼叫方法會被呼叫一次;計時器的週期行為會被停用,但可透過呼叫 Change 並指定正值來 period重新啟用。

Change這個方法可以由TimerCallback代理人呼叫。

另請參閱

適用於

Change(TimeSpan, TimeSpan)

改變計時器的開始時間及方法呼叫間隔,並利用 TimeSpan 數值來衡量時間區間。

public:
 bool Change(TimeSpan dueTime, TimeSpan period);
public bool Change(TimeSpan dueTime, TimeSpan period);
member this.Change : TimeSpan * TimeSpan -> bool
Public Function Change (dueTime As TimeSpan, period As TimeSpan) As Boolean

參數

dueTime
TimeSpan

A TimeSpan 代表在呼叫回調方法前的延遲時間,該方法是在建構時 Timer 指定的。 指定 InfiniteTimeSpan 防止計時器重新啟動。 請指定 Zero 立即重新啟動計時器。

period
TimeSpan

回調方法呼叫間隔的時間間隔決定了建構時間 Timer 。 指定 InfiniteTimeSpan 關閉週期性訊號。

傳回

true如果計時器成功更新;否則,。 false

例外狀況

這些 Timer 已經被處理掉了。

dueTimeperiod參數(以毫秒計)小於 -1。

dueTimeperiod參數(以毫秒計)大於4294967294。

範例

以下程式碼範例示範如何啟動一個 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

備註

回調方法在經過後 dueTime 會被呼叫一次,之後則在經過時間間隔 period 內呼叫一次。

dueTimeTimeSpan.Zero,則會立即呼叫回調方法。 若 dueTimeInfiniteTimeSpan則回調方法永遠不會被呼叫;計時器會被停用,但可透過呼叫 Change 並指定正值來 dueTime重新啟用。

periodTimeSpan.ZeroInfiniteTimeSpan,且 dueTime 為正數,則呼叫方法會被呼叫一次;計時器的週期性行為會被停用,但可透過呼叫 Change 並指定大於零的 period值來重新啟用。

Change這個方法可以由TimerCallback代理人呼叫。

另請參閱

適用於

Change(UInt32, UInt32)

重要

此 API 不符合 CLS 規範。

改變計時器的開始時間及方法呼叫間隔,使用 32 位元無符號整數來測量時間區間。

public:
 bool Change(System::UInt32 dueTime, System::UInt32 period);
[System.CLSCompliant(false)]
public bool Change(uint dueTime, uint period);
[<System.CLSCompliant(false)>]
member this.Change : uint32 * uint32 -> bool
Public Function Change (dueTime As UInteger, period As UInteger) As Boolean

參數

dueTime
UInt32

呼叫回撥方法前的延遲時間,以毫秒為單位,決定了該方法的建構時間 Timer 。 指定 Infinite 防止計時器重新啟動。 指定零(0)即可立即重新啟動計時器。

period
UInt32

回調方法呼叫間隔的時間區間,以毫秒為單位,表示該方法的建構時間 Timer 。 指定 Infinite 關閉週期性訊號。

傳回

true如果計時器成功更新;否則,。 false

屬性

例外狀況

這些 Timer 已經被處理掉了。

備註

回調方法在經過後 dueTime 會被呼叫一次,之後則在經過時間間隔 period 內呼叫一次。

dueTime 為零(0),則會立即呼叫回調方法。 若 dueTimeTimeout.Infinite則回調方法永遠不會被呼叫;計時器會被停用,但可透過呼叫 Change 並指定正值來 dueTime重新啟用。

period 為零(0)或 Timeout.Infinite,且 dueTimeTimeout.Infinite,則呼叫方法會被呼叫一次;計時器的週期行為會被停用,但可透過呼叫 Change 並指定正值來 period重新啟用。

Change這個方法可以由TimerCallback代理人呼叫。

另請參閱

適用於