C#中的等待定时器? [英] Waitable Timer in C# ?

查看:131
本文介绍了C#中的等待定时器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自Win32世界。现在,我将现有代码移植到C#。我用
在C#中找不到等效的Win32 Waitable Timer。我有一个帖子

定期执行一些任务如下:


虽然(...)

{

做点什么...


SetWaitableTimer(hTimer,...);

WaitForSingleObject(hTimer,...);

}


有人可以告诉我如何实现一个等待计时器,这样一个线程可以实际等待它吗?b / b实际上等待它吗?


谢谢!

-

Eric

I''m coming from Win32 world. Now, I''m porting the existing code to C#. I
cannot find the equivalent Win32 Waitable Timer in C#. I have a thread to
perform some task periodically like following:

While(...)
{
Do something ...

SetWaitableTimer(hTimer, ...);
WaitForSingleObject(hTimer, ...);
}

Could anybody tell me how to implement a waitable timer so a thread can
actually wait for it?

Thanks!
--
Eric

推荐答案

2007年4月18日星期三09:38:01 -0700,Eric< Er ** @ discussion.microsoft.com>

写道:
On Wed, 18 Apr 2007 09:38:01 -0700, Eric <Er**@discussions.microsoft.com>
wrote:

我来自Win32世界。现在,我将现有代码移植到C#。我用
在C#中找不到等效的Win32 Waitable Timer。我有一个帖子

定期执行一些任务如下:


虽然(...)

{

做点什么...


SetWaitableTimer(hTimer,...);

WaitForSingleObject(hTimer,...);

}


可以有人告诉我如何实现一个等待计时器,这样一个线程可以实际等待它吗?
I''m coming from Win32 world. Now, I''m porting the existing code to C#. I
cannot find the equivalent Win32 Waitable Timer in C#. I have a thread to
perform some task periodically like following:

While(...)
{
Do something ...

SetWaitableTimer(hTimer, ...);
WaitForSingleObject(hTimer, ...);
}

Could anybody tell me how to implement a waitable timer so a thread can
actually wait for it?



我不知道任何完全复制功能的.NET类。

但是,你真的需要APC行为吗? Win32等待计时器

对象?也就是说,重要的是计时器完成例程

在同一个线程上执行,但代码中没有显式调用?


在.NET中,你有其他计时选项。 System.Timers.Timer和

System.Threading.Timer都提供类似的计时功能,

的主要区别在于当计时器触发给予
计时器在另一个线程上运行。如果您真正想要的是等待特定时间,那么您可以使用Sleep()调用。如果你确实想要在等待另一个线程运行定时器委托时阻止
,那么你可以等待一个事件实例并让定时器委托设置

活动。


所以,根据你真正需要的功能,你要么不能用.NET实现它的价值(AFAIK)或者你可以用稍微不同的方式做到这一点。对我来说似乎是b / b
在大多数情况下,一个应用程序并不真的需要一个Win32

等待计时器对象,但你可能正在移植一个罕见的块

代码。准确地知道计时器的用途是什么?

有助于为您提供有用的选项。


Pete

I don''t know of any .NET class that duplicate the functionality exactly.
However, do you really need the APC behavior of the Win32 waitable timer
object? That is, is it important that the timer completion routine
execute on the same thread but without an explicit call in your code?

In .NET, you do have other timing options. System.Timers.Timer and
System.Threading.Timer both provide similar timing functionality, with the
main difference being that when the timer fires the delegate given to the
timer is run on another thread. If all you really want is to wait a
specific time, then you could just use a call to Sleep(). If you really
want to block while waiting for another thread to run the timer delegate,
you could wait on an event instance and have the timer delegate set the
event.

So, depending on what functionality you really need, you either can''t do
it in .NET (AFAIK) or you can do it in slightly different ways. It seems
to me that in most cases, an application doesn''t really need a Win32
waitable timer object, but you could be porting one of the rare blocks of
code that does. Knowing exactly what the timer''s being used for would be
helpful in providing useful options for you.

Pete


对于初学者,我会看看ManualResetEvent类,它是各种

WaitOne等重载方法。

彼得


-

网站: http://www.eggheadcafe.com

UnBlog: http://petesbloggerama.blogspot.com

短网址&更多: http://ittyurl.net


埃里克写道:
For starters, I''d take a look at the ManualResetEvent class and it''s various
WaitOne, etc. overloaded methods.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Eric" wrote:

我来自Win32世界。现在,我将现有代码移植到C#。我用
在C#中找不到等效的Win32 Waitable Timer。我有一个帖子

定期执行一些任务如下:


虽然(...)

{

做点什么...


SetWaitableTimer(hTimer,...);

WaitForSingleObject(hTimer,...);

}


有人可以告诉我如何实现一个等待计时器,这样一个线程可以实际等待它吗?b / b实际上等待它吗?


谢谢!

-

Eric
I''m coming from Win32 world. Now, I''m porting the existing code to C#. I
cannot find the equivalent Win32 Waitable Timer in C#. I have a thread to
perform some task periodically like following:

While(...)
{
Do something ...

SetWaitableTimer(hTimer, ...);
WaitForSingleObject(hTimer, ...);
}

Could anybody tell me how to implement a waitable timer so a thread can
actually wait for it?

Thanks!
--
Eric


我想关于以下列方式执行此操作:


设置事件

WaitOne()事件并设置其中的时间间隔以使等待到期

模拟等待计时器。


或者,甚至使用Sleep。


但是,计时器在WaitOne和Sleep中,并不像在

Win32等待计时器中那样准确。这个

应用程序的55ms分辨率还不够好。它必须在10ms内。


Yi

-

Eric

" Peter Bromberg [C#MVP]"写道:
I thought about doing that in the following way:

Set the event
WaitOne() the event and set the time interval in it to let the wait expire
to simulate the waitable timer.

or, even use the Sleep.

But, the "timer" inside WaitOne and Sleep are not as accurate as in the
Win32 waitable timer. The 55ms resolution is not good enough for this
application. It has to be within 10ms.

Yi
--
Eric
"Peter Bromberg [C# MVP]" wrote:

对于初学者,我会看看ManualResetEvent类,它是各种各样的

WaitOne等。重载方法。

彼得


-

网站: http://www.eggheadcafe.com

UnBlog: http://petesbloggerama.blogspot.com

短网址&更多: http://ittyurl.net


埃里克写道:
For starters, I''d take a look at the ManualResetEvent class and it''s various
WaitOne, etc. overloaded methods.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Eric" wrote:

我来自Win32世界。现在,我将现有代码移植到C#。我用
在C#中找不到等效的Win32 Waitable Timer。我有一个帖子

定期执行一些任务如下:


虽然(...)

{

做点什么...


SetWaitableTimer(hTimer,...);

WaitForSingleObject(hTimer,...);

}


有人可以告诉我如何实现一个等待计时器,这样一个线程可以实际等待它吗?b / b实际上等待它吗?


谢谢!

-

Eric
I''m coming from Win32 world. Now, I''m porting the existing code to C#. I
cannot find the equivalent Win32 Waitable Timer in C#. I have a thread to
perform some task periodically like following:

While(...)
{
Do something ...

SetWaitableTimer(hTimer, ...);
WaitForSingleObject(hTimer, ...);
}

Could anybody tell me how to implement a waitable timer so a thread can
actually wait for it?

Thanks!
--
Eric


这篇关于C#中的等待定时器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆