TimerCallback 委托
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示处理来自 Timer 的调用的方法。
public delegate void TimerCallback(System::Object ^ state);
[System.Runtime.InteropServices.ComVisible(true)]
public delegate void TimerCallback(object state);
public delegate void TimerCallback(object? state);
public delegate void TimerCallback(object state);
[<System.Runtime.InteropServices.ComVisible(true)>]
type TimerCallback = delegate of obj -> unit
type TimerCallback = delegate of obj -> unit
Public Delegate Sub TimerCallback(state As Object)
参数
- state
- Object
一个对象(包含与该委托所调用的方法相关的特定于应用程序的信息)或为 null
。
- 属性
示例
下面的代码示例演示如何创建与 类一起使用的 Timer
委托。
using namespace System;
using namespace System::Threading;
ref class StatusChecker
{
private:
int invokeCount, maxCount;
public:
StatusChecker(int count)
{
invokeCount = 0;
maxCount = count;
}
// This method is called by the timer delegate.
void CheckStatus(Object^ stateInfo)
{
AutoResetEvent^ autoEvent = dynamic_cast<AutoResetEvent^>(stateInfo);
Console::WriteLine("{0:h:mm:ss.fff} Checking status {1,2}.",
DateTime::Now, ++invokeCount);
if (invokeCount == maxCount) {
// Reset the counter and signal the waiting thread.
invokeCount = 0;
autoEvent->Set();
}
}
};
ref class TimerExample
{
public:
static void Main()
{
// Create an AutoResetEvent to signal the timeout threshold in the
// timer callback has been reached.
AutoResetEvent^ autoEvent = gcnew AutoResetEvent(false);
StatusChecker^ statusChecker = gcnew StatusChecker(10);
// Create a delegate that invokes methods for the timer.
TimerCallback^ tcb =
gcnew TimerCallback(statusChecker, &StatusChecker::CheckStatus);
// 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);
Timer^ stateTimer = gcnew Timer(tcb, autoEvent, 1000, 250);
// When autoEvent signals, change the period to every half second.
autoEvent->WaitOne(5000, false);
stateTimer->Change(0, 500);
Console::WriteLine("\nChanging period to .5 seconds.\n");
// When autoEvent signals the second time, dispose of the timer.
autoEvent->WaitOne(5000, false);
stateTimer->~Timer();
Console::WriteLine("\nDestroying timer.");
}
};
int main()
{
TimerExample::Main();
}
// 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.
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.
注解
TimerCallback
使用委托指定由 Timer调用的方法。 此方法不会在创建计时器的线程中执行;它在系统提供的单独线程池线程中执行。 委托 TimerCallback
在开始时间过后调用方法一次,并按计时器间隔继续调用该方法一次,直到 Dispose 调用方法,或直到 Timer.Change 使用间隔值 Infinite调用方法。
注意
调用方法重载后 Dispose() 可能会发生回调,因为计时器会将回调排入队列,以便线程池线程执行。 可以使用 Dispose(WaitHandle) 方法重载等待所有回调完成。
计时器委托是在构造计时器时指定的,不能更改。 的开始时间Timer
在构造函数的 Timer
参数中dueTime
传递,期间在 参数中period
传递。 有关演示如何创建和使用 TimerCallback
委托的示例,请参阅 System.Threading.Timer。
扩展方法
GetMethodInfo(Delegate) |
获取指示指定委托表示的方法的对象。 |