473,396 Members | 1,804 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Waitable Timer in C# ?

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
Apr 18 '07 #1
11 12748
On Wed, 18 Apr 2007 09:38:01 -0700, Eric <Er**@discussions.microsoft.com>
wrote:
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?
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
Apr 18 '07 #2
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:
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
Apr 18 '07 #3
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:
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:
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
Apr 18 '07 #4
>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.

I like this idea! I think it is a great way to simulate the Win32 waitable
timer behavior by combining the .Net timer and the event object.

--
Eric
"Peter Duniho" wrote:
On Wed, 18 Apr 2007 09:38:01 -0700, Eric <Er**@discussions.microsoft.com>
wrote:
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?

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
Apr 18 '07 #5
On Wed, 18 Apr 2007 11:08:28 -0700, Eric <Er**@discussions.microsoft.com>
wrote:
>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.

I like this idea! I think it is a great way to simulate the Win32
waitable timer behavior by combining the .Net timer and the event
object.
Yes, but if all you're doing is causing the thread to pause and then
resume after a specific amount of time, all you really need is to call
Sleep().

I mean, I'm glad you liked my suggestion and all, but please don't use it
unless you really need to do some processing in the callback other than
releasing the thread. :)

Pete
Apr 18 '07 #6
On Wed, 18 Apr 2007 11:00:00 -0700, Eric <Er**@discussions.microsoft.com>
wrote:
[...]
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.
I don't know that you'll get better than that from .NET. Even if you use
the suggestion to have the Timer class set an event, and even if the Timer
class offers the resolution you want (and I don't know that it does),
because it's releasing a different thread by setting the event handle, you
have to deal with the thread scheduling and the resulting inaccuracy (that
is, the thread being released may or may not run the instant its event
handle is set).

Pete
Apr 18 '07 #7
Does Sleep() have the resolution around 55ms?
--
Eric
"Peter Duniho" wrote:
On Wed, 18 Apr 2007 11:08:28 -0700, Eric <Er**@discussions.microsoft.com>
wrote:
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.

I like this idea! I think it is a great way to simulate the Win32
waitable timer behavior by combining the .Net timer and the event
object.

Yes, but if all you're doing is causing the thread to pause and then
resume after a specific amount of time, all you really need is to call
Sleep().

I mean, I'm glad you liked my suggestion and all, but please don't use it
unless you really need to do some processing in the callback other than
releasing the thread. :)

Pete
Apr 19 '07 #8
You got me thinking here. I planed to use a manual event and a system timer ,
which according to the .NET document, has the high resolution. Adding the
thread context switching time, it might introduce some latency.....

--
Eric
"Peter Duniho" wrote:
On Wed, 18 Apr 2007 11:00:00 -0700, Eric <Er**@discussions.microsoft.com>
wrote:
[...]
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.

I don't know that you'll get better than that from .NET. Even if you use
the suggestion to have the Timer class set an event, and even if the Timer
class offers the resolution you want (and I don't know that it does),
because it's releasing a different thread by setting the event handle, you
have to deal with the thread scheduling and the resulting inaccuracy (that
is, the thread being released may or may not run the instant its event
handle is set).

Pete
Apr 19 '07 #9
On Thu, 19 Apr 2007 08:18:16 -0700, Eric <Er**@discussions.microsoft.com>
wrote:
Does Sleep() have the resolution around 55ms?
The resolution of Sleep() depends on the resolution of the system clock.

Using native Windows API, you can use timeGetDevCaps to find the current
resolution of the timer, and timeBeginPeriod to adjust it. I don't know
whether similar access to the timer resolution is available in .NET.
Maybe through WMI? Not something I've done before so it's not something
I'm familiar with.

All that said, you should of course always keep in mind that whatever the
resolution of the timer being used, because of the way Windows handles
thread scheduling, there is *never* a guarantee that you will get to run
your thread exactly when you want to.

Pete
Apr 19 '07 #10
"Eric" <Er**@discussions.microsoft.comwrote in message
news:F1**********************************@microsof t.com...
Does Sleep() have the resolution around 55ms?
--
Sleep has no real resolution, calling Sleep gives up the current thread's quantum and puts
the thread asleep for aprox. the time specified as argument (if NOT 0), or, if the time
specified is lower than a thread's quantum, for the remaining of that quantum.
A thread's quantum is a multiple of the RTC interval, on modern systems this interval is per
default, 10 or 15.6 msecs. (multi-cpu) and can be determined by calling
GetSystemTimeAdjustment. The interval can also be changed on a per thread basis by calling
"timeBeginPeriod" paired with "timeEndPeriod" Multimedia Timer API's.

Say you have a Thread quantum of 10msecs. and a thread that has run for 3msecs. before
calling Sleep(5), in this case the thread will sleep for at least 10 - 3 = 7 msecs.
A Sleep(18) on a system with 15.6 msecs interval, will sleep for anything between 15.6 and
31.2 msecs.
Notice I said "at least", this is not guaranteed, other runable threads with higher or equal
priorities can drastically increase the total sleep time of your thread.

Willy.

Apr 19 '07 #11
Thank you for the clear description!

Yi
--
Eric
"Willy Denoyette [MVP]" wrote:
"Eric" <Er**@discussions.microsoft.comwrote in message
news:F1**********************************@microsof t.com...
Does Sleep() have the resolution around 55ms?
--

Sleep has no real resolution, calling Sleep gives up the current thread's quantum and puts
the thread asleep for aprox. the time specified as argument (if NOT 0), or, if the time
specified is lower than a thread's quantum, for the remaining of that quantum.
A thread's quantum is a multiple of the RTC interval, on modern systems this interval is per
default, 10 or 15.6 msecs. (multi-cpu) and can be determined by calling
GetSystemTimeAdjustment. The interval can also be changed on a per thread basis by calling
"timeBeginPeriod" paired with "timeEndPeriod" Multimedia Timer API's.

Say you have a Thread quantum of 10msecs. and a thread that has run for 3msecs. before
calling Sleep(5), in this case the thread will sleep for at least 10 - 3 = 7 msecs.
A Sleep(18) on a system with 15.6 msecs interval, will sleep for anything between 15.6 and
31.2 msecs.
Notice I said "at least", this is not guaranteed, other runable threads with higher or equal
priorities can drastically increase the total sleep time of your thread.

Willy.

Apr 20 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dinesh Eswaran | last post by:
hi all, I want to create a waitable timer in C#. How do I do it? There is Timer class and there is the WaitHandle class with WaitAny... methods. But they dont seem to help. Basically iam looking...
9
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the following manner: System.Threading.Timer m_timer = null;...
2
by: John David Thornton | last post by:
I've got a Windows Service class, and I put a System.Threading.Timer, and I've coded it as shown below. However, when I install the service and then start it in MMC, I get a peculiar message: ...
5
by: Lonewolf | last post by:
Hi all, I am not sure if it is even possible to do it from .NET itself. Is there something like a waitable timer which can resume the system from its hibernate or standby (S3: suspend to ram)...
2
by: Mike | last post by:
Hello everyone: I am looking for some VB.NET code samples that make use of waitable timers that put the thread of the application to Sleep for the specificied period of time. I am told that the...
0
by: Sam | last post by:
Hello: Can anyone provide me some idea on how to replace the Sleep method. Major Disadvantage that I have been facing with the Sleep method is that the application freezes during the sleep time...
12
by: Gina_Marano | last post by:
I have created an array of timers (1-n). At first I just created windows form timers but I read that system timers are better for background work. The timers will just be monitoring different...
8
by: =?Utf-8?B?RGF2ZSBCb29rZXI=?= | last post by:
I have a Timer that I set to go off once a day, but it frequently fails! In order to debug I would like to be able to check, at any moment, whether the Timer is enabled and when it will next...
16
by: Peter Oliphant | last post by:
Note that although this involves SAPI, it is more a question about Timers and event handlers. I wrote a Speech Recognize handler (SAPI), and put some code in it to enable a Timer. It would not...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.