移植SetTimer的()和KillTimer函数()到C#? [英] Porting SetTimer() and KillTimer() to C#?
问题描述
我想端口从C ++到C#的一些代码
I am trying to port some code from C++ to C#.
我在C ++代码碰到这个就来了:
I came across this in the C++ code:
watchdogTimer = SetTimer(1,1000,NULL);
...
KillTimer(watchdogTimer);
这是什么代码做的,这可怎么移植到C#?
What is this code doing, and how can this be ported to C#?
感谢。
推荐答案
的的CWnd :: SetTimer的$你看C $ C>函数创建发送
WM_TIMER
事件窗口的计时器。这是类似于 System.Windows.Forms.Timer 在.NET组件。它的行为比 System.Timers.Timer
有所不同。有两个区别是特别重要:
The CWnd::SetTimer
function you're looking at creates a timer that sends WM_TIMER
events to the window. This is analogous to the System.Windows.Forms.Timer component in .NET. It behaves somewhat differently than the System.Timers.Timer
. There are two differences that are particularly relevant:
Windows.Forms.Timer
调用UI线程上的事件处理程序。默认情况下, System.Timers.Timer
调用线程池,线程事件处理程序。您可以使用 SynchronizingObject的财产有<在UI线程上code> System.Timers.Timer 调用。
Windows.Forms.Timer
calls the event handler on the UI thread. By default, System.Timers.Timer
calls the event handler on a threadpool thread. You can use SynchronizingObject property to have the System.Timers.Timer
call on the UI thread.
另一个不同是,它不是可能遇到重入问题Windows窗体计时器,因为Windows将不允许多个 WM_TIMER
从队列中的同一个定时器消息,也不会放置 WM_TIMER
队列中的消息,如果一个已经被处理。这通常是一件好事。
Another difference is that it's not possible to encounter reentrancy problems with the Windows Forms timer because Windows won't allow multiple WM_TIMER
messages from the same timer in the queue, nor will it place a WM_TIMER
message in the queue if one is already being processed. This is generally a good thing.
System.Timers.Timer
,在另一方面,将允许重入。所以,如果你的计时器事件处理程序比定时器周期需要更长的时间,你可以同时处理多个事件相同的定时器。如果你的定时器周期为100毫秒,处理需要150毫秒,你会得到一个通知,当你处理的第一个。如果您使用 SynchronizingObject的
来强制UI线程上的回调,这可能会导致一大堆悬而未决的回调排队。
System.Timers.Timer
, on the other hand, will allow reentrancy. So if your timer event handler takes longer than the timer period, you can be processing multiple events for the same timer concurrently. If your timer period is 100 ms and processing takes 150 ms, you're going to get another notification while you're processing the first one. If you use the SynchronizingObject
to force the callback on the UI thread, this can lead to a whole bunch of pending callbacks being queued.
两个定时器的实现是完全不同的。 Windows窗体定时器使用旧式的Windows计时器一>已经存在了20年。这种类型的计时器的需要的窗口句柄和一个消息循环,因此,仅在GUI程序中使用。 System.Timers.Timer
约为 System.Threading.Timer
一个瘦包装,它使用Windows的线程池计时器。
The implementation of the two timers is quite different. The Windows Forms timer uses old style Windows timers that have been around for 20 years. This type of timer requires a window handle and a message loop, and is therefore used only in GUI programs. System.Timers.Timer
is a thin wrapper around System.Threading.Timer
, which uses the Windows Thread Pool Timers.
这篇关于移植SetTimer的()和KillTimer函数()到C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!