Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - How to Make a .NET Timer Always Fire at the Right Time

Reply
 
Old 4 Weeks Ago   #11 (permalink)
Patrice


 
 

Re: How to Make a .NET Timer Always Fire at the Right Time

> That's assuming your protocol actually needs keepalives, as many people
Quote:

> forget that TCP's ability to keep a connection open without traffic is a
> feature, not a bug.
It begins to be OT respective with the OP initial demand by may I ask what
you think about the connection being closed after 10 s ? Is this something
usual ?

TIA

--
Patrice


My System SpecsSystem Spec
Old 4 Weeks Ago   #12 (permalink)
Jeroen Mostert


 
 

Re: How to Make a .NET Timer Always Fire at the Right Time

Patrice wrote:
Quote:
Quote:

>> That's assuming your protocol actually needs keepalives, as many
>> people forget that TCP's ability to keep a connection open without
>> traffic is a feature, not a bug.
>
> It begins to be OT respective with the OP initial demand by may I ask
> what you think about the connection being closed after 10 s ? Is this
> something usual ?
>
I have worked with setups where the server side closed the connection after
just 20 seconds of inactivity, because it was more important for the
connection to be "known good" even at short intervals than it was to be
mindful of the bandwidth wasted this way.

I'm not saying this is a good way of doing things (using short transmission
timeouts is probably a better idea), but I don't think it's unusual either.

--
J.
My System SpecsSystem Spec
Old 4 Weeks Ago   #13 (permalink)
Jeroen Mostert


 
 

Re: How to Make a .NET Timer Always Fire at the Right Time

Charles wrote:
Quote:

> I've been wondering how I might increase the priority of the the thread
> that the timer runs on, but if it uses the thread pool then I don't
> imagine I can increase the priority?
No, you can't. Well, to be precise, you can, but there's no thread affinity
for the timer, so you'll just be boosting the priority of some random thread
that's handling the timer callback.

I don't recall if it's possible for the thread that sets the timer to get
the callback if the interval is sufficiently short, but in any case that
won't apply here since your interval is in the range of seconds.
Quote:

> If that's the case, then perhaps that's another reason for going for a
> waitable timer, as the callback runs on the same thread on which the
> timer was created and I could boost the priority of that thread. Does
> that sound reasonable?
>
Yes, in principle. There's one caveat with waitable timers, though: they use
APCs to signal completion. This is slightly tricky because the CLR also uses
APCs to communicate with threads (Thread.Interrupt() and Thread.Abort(),
among others, queue an APC on the thread). I recall having some problems
using APCs in managed threads, but nothing specific; it's probably the
general trouble of having to be careful in callbacks from unmanaged code,
especially something as low-level as APCs. I do recall you can use

try {
Thread.Sleep(Timeout.Infinite);
} catch (InterruptedException) {
return; // asked to stop
}

And then call Thread.Interrupt() when your thread should stop.
Thread.Sleep() uses SleepEx() under the covers and will properly respond to
the CLR signaling APCs.

I'd try timer queues first. By default these just use worker threads, so no
mucking around with APCs. They don't allow for boosting priority but they
don't use the .NET thread pool either, so they might be less susceptible to
preemption under load. I have no idea if this is actually the case, but
you'll have to thoroughly test whatever solution you pick anyway, so you
might as well start with the easier ones.

Speaking of which, you really are better off dedicating a machine that's
guaranteed to have low loads to this task, like Mike suggested. :-) If you
can isolate the processes that are ruining it for the rest, you could try
looking into job objects to limit the memory and CPU they're assigned.
Keeping your program simple and not overly reliant on interesting low-level
techniques is certainly worth some effort.

--
J.
My System SpecsSystem Spec
Old 4 Weeks Ago   #14 (permalink)
Jesse Houwing


 
 

Re: How to Make a .NET Timer Always Fire at the Right Time

* Charles wrote, On 1-11-2009 17:59:
Quote:

> Hi Mike
>
> Thanks for the quick reply. It's not critical that it is exactly 10
> seconds. 12 seconds would do, or even 15 seconds occasionally. But what
> I can't have is it being 30 seconds between timer events, and that is
> the worst cases I have seen. I have to send a message over TCP/IP about
> every 10 seconds in order to keep a connection open, otherwise the other
> end shuts up shop and goes away.
Isn't it possible to make that connection more reliable (by leaving it
open longer), or wouldn't it just be possible to switch to UDP instead
and forget the notion of a connection altogether?

Jesse
Quote:

>
> Charles
>
>
> "Family Tree Mike" <FamilyTreeMike@newsgroup> wrote in message
> news:#QdxhSvWKHA.1792@newsgroup
Quote:

>> Charles wrote:
Quote:

>>> This is a follow up to an earlier post, about a Threading.Timer that
>>> occasionally fired at odd times. In that case I discovered that low
>>> memory meant that the machine 'froze' intermittently and a timer
>>> callback could fire after 30 seconds instead of every 10 seconds as
>>> intended.
>>>
>>> I now find that if the machine becomes preoccupied with another task,
>>> I get the same effect. This is a very bad state of affairs, as I can
>>> no longer rely on my 10 second tick occurring every 10 seconds.
>>>
>>> I need to have a reliable 10 second timer, such that an event happens
>>> every 10 seconds. It's no good if I get two events after 20 seconds,
>>> I need one every 10 seconds.
>>>
>>> How is this possible to guarantee in .NET? The app is running on
>>> Windows Server 2003 x64.
>>>
>>> TIA
>>>
>>> Charles
>>>
>>>
>>
>> If the task is that critical that it _must_ occur every 10 seconds,
>> then I would dedicate a machine to that task. If that task is it's
>> job, then it does nothing else.
>>
>> It is impossible for me to know your situation, but is there a way to
>> "throttle" the other process that is grabbing the CPU attention? That
>> may be an option to look into.
>>
>> --
>> Mike
>

--
Jesse Houwing
jesse.houwing at sogeti.nl
My System SpecsSystem Spec
Old 4 Weeks Ago   #15 (permalink)
Charles


 
 

Re: How to Make a .NET Timer Always Fire at the Right Time

Hi Jesse

Because it's third-party, I'm at their mercy. This is just the protocol I
have to accommodate.

Charles


"Jesse Houwing" <jesse.houwing@newsgroup> wrote in message
news:#l#KjB0WKHA.3428@newsgroup
Quote:

> * Charles wrote, On 1-11-2009 17:59:
Quote:

>> Hi Mike
>>
>> Thanks for the quick reply. It's not critical that it is exactly 10
>> seconds. 12 seconds would do, or even 15 seconds occasionally. But what
>> I can't have is it being 30 seconds between timer events, and that is
>> the worst cases I have seen. I have to send a message over TCP/IP about
>> every 10 seconds in order to keep a connection open, otherwise the other
>> end shuts up shop and goes away.
>
> Isn't it possible to make that connection more reliable (by leaving it
> open longer), or wouldn't it just be possible to switch to UDP instead and
> forget the notion of a connection altogether?
>
> Jesse
>
Quote:

>>
>> Charles
>>
>>
>> "Family Tree Mike" <FamilyTreeMike@newsgroup> wrote in message
>> news:#QdxhSvWKHA.1792@newsgroup
Quote:

>>> Charles wrote:
>>>> This is a follow up to an earlier post, about a Threading.Timer that
>>>> occasionally fired at odd times. In that case I discovered that low
>>>> memory meant that the machine 'froze' intermittently and a timer
>>>> callback could fire after 30 seconds instead of every 10 seconds as
>>>> intended.
>>>>
>>>> I now find that if the machine becomes preoccupied with another task,
>>>> I get the same effect. This is a very bad state of affairs, as I can
>>>> no longer rely on my 10 second tick occurring every 10 seconds.
>>>>
>>>> I need to have a reliable 10 second timer, such that an event happens
>>>> every 10 seconds. It's no good if I get two events after 20 seconds,
>>>> I need one every 10 seconds.
>>>>
>>>> How is this possible to guarantee in .NET? The app is running on
>>>> Windows Server 2003 x64.
>>>>
>>>> TIA
>>>>
>>>> Charles
>>>>
>>>>
>>>
>>> If the task is that critical that it _must_ occur every 10 seconds,
>>> then I would dedicate a machine to that task. If that task is it's
>>> job, then it does nothing else.
>>>
>>> It is impossible for me to know your situation, but is there a way to
>>> "throttle" the other process that is grabbing the CPU attention? That
>>> may be an option to look into.
>>>
>>> --
>>> Mike
>>
>
>
> --
> Jesse Houwing
> jesse.houwing at sogeti.nl
My System SpecsSystem Spec
Old 4 Weeks Ago   #16 (permalink)
James Hahn


 
 

Re: How to Make a .NET Timer Always Fire at the Right Time

If your timer can't respond within, say 5s of the nominated interval, then
surely that indicates that the machine is not responsive, and that is
exactly why the server needs that acknowledgement. If you manage to force
the keepalive even when the machine is bogged down like this, surely you
will risk corrupting the connection because the server is being fooled into
thinking that the connection is active when it isn't.

"Charles" <blank@newsgroup> wrote in message
news:eG3x2DvWKHA.4780@newsgroup
Quote:

> This is a follow up to an earlier post, about a Threading.Timer that
> occasionally fired at odd times. In that case I discovered that low memory
> meant that the machine 'froze' intermittently and a timer callback could
> fire after 30 seconds instead of every 10 seconds as intended.
>
> I now find that if the machine becomes preoccupied with another task, I
> get the same effect. This is a very bad state of affairs, as I can no
> longer rely on my 10 second tick occurring every 10 seconds.
>
> I need to have a reliable 10 second timer, such that an event happens
> every 10 seconds. It's no good if I get two events after 20 seconds, I
> need one every 10 seconds.
>
> How is this possible to guarantee in .NET? The app is running on Windows
> Server 2003 x64.
>
> TIA
>
> Charles
>
>
My System SpecsSystem Spec
Old 4 Weeks Ago   #17 (permalink)
Charles


 
 

Re: How to Make a .NET Timer Always Fire at the Right Time

Hi James

Actually, you have a point there. I hadn't thought of it like that. I should
perhaps put more effort into re-establishing the connection when the machine
unfreezes.

On the other side though, the connection is important enough to keep going
at all costs, so if I could make my task higher priority then it would be ok
if other processes suffered slightly.

Charles


"James Hahn" <jhahn@newsgroup> wrote in message
news:uf2SDi1WKHA.3720@newsgroup
Quote:

> If your timer can't respond within, say 5s of the nominated interval, then
> surely that indicates that the machine is not responsive, and that is
> exactly why the server needs that acknowledgement. If you manage to force
> the keepalive even when the machine is bogged down like this, surely you
> will risk corrupting the connection because the server is being fooled
> into thinking that the connection is active when it isn't.
>
> "Charles" <blank@newsgroup> wrote in message
> news:eG3x2DvWKHA.4780@newsgroup
Quote:

>> This is a follow up to an earlier post, about a Threading.Timer that
>> occasionally fired at odd times. In that case I discovered that low
>> memory meant that the machine 'froze' intermittently and a timer callback
>> could fire after 30 seconds instead of every 10 seconds as intended.
>>
>> I now find that if the machine becomes preoccupied with another task, I
>> get the same effect. This is a very bad state of affairs, as I can no
>> longer rely on my 10 second tick occurring every 10 seconds.
>>
>> I need to have a reliable 10 second timer, such that an event happens
>> every 10 seconds. It's no good if I get two events after 20 seconds, I
>> need one every 10 seconds.
>>
>> How is this possible to guarantee in .NET? The app is running on Windows
>> Server 2003 x64.
>>
>> TIA
>>
>> Charles
>>
>>
>
My System SpecsSystem Spec
Old 4 Weeks Ago   #18 (permalink)
Charles


 
 

Re: How to Make a .NET Timer Always Fire at the Right Time

It's not really practical to dedicate a machine to this task, and in any
case, if I could assign an appropriate level of priority to the task then
any other tasks would get plenty of time to operate.

The thing that currently seems to upset it is file transfers over RDP. I
occasionally need to copy a file to the machine over RDP, which can take
anything from 1 minute to 30 minutes. There are times in this window when
the machine appears to hang, but I imagine it is just concentrating really
hard on the file transfer. Anyway, that's when my timer goes astray. Giving
a regular 10 second slot to my task would not hinder the file transfer in
any way, and even if it did, I could live with it. It's just a case of
priorities, as it were.

Charles


"Jeroen Mostert" <jmostert@newsgroup> wrote in message
news:4aedf426$0$83239$e4fe514c@newsgroup
Quote:

> Charles wrote:
Quote:

>> I've been wondering how I might increase the priority of the the thread
>> that the timer runs on, but if it uses the thread pool then I don't
>> imagine I can increase the priority?
>
> No, you can't. Well, to be precise, you can, but there's no thread
> affinity for the timer, so you'll just be boosting the priority of some
> random thread that's handling the timer callback.
>
> I don't recall if it's possible for the thread that sets the timer to get
> the callback if the interval is sufficiently short, but in any case that
> won't apply here since your interval is in the range of seconds.
>
Quote:

>> If that's the case, then perhaps that's another reason for going for a
>> waitable timer, as the callback runs on the same thread on which the
>> timer was created and I could boost the priority of that thread. Does
>> that sound reasonable?
>>
> Yes, in principle. There's one caveat with waitable timers, though: they
> use APCs to signal completion. This is slightly tricky because the CLR
> also uses APCs to communicate with threads (Thread.Interrupt() and
> Thread.Abort(), among others, queue an APC on the thread). I recall having
> some problems using APCs in managed threads, but nothing specific; it's
> probably the general trouble of having to be careful in callbacks from
> unmanaged code, especially something as low-level as APCs. I do recall you
> can use
>
> try {
> Thread.Sleep(Timeout.Infinite);
> } catch (InterruptedException) {
> return; // asked to stop
> }
>
> And then call Thread.Interrupt() when your thread should stop.
> Thread.Sleep() uses SleepEx() under the covers and will properly respond
> to the CLR signaling APCs.
>
> I'd try timer queues first. By default these just use worker threads, so
> no mucking around with APCs. They don't allow for boosting priority but
> they don't use the .NET thread pool either, so they might be less
> susceptible to preemption under load. I have no idea if this is actually
> the case, but you'll have to thoroughly test whatever solution you pick
> anyway, so you might as well start with the easier ones.
>
> Speaking of which, you really are better off dedicating a machine that's
> guaranteed to have low loads to this task, like Mike suggested. :-) If you
> can isolate the processes that are ruining it for the rest, you could try
> looking into job objects to limit the memory and CPU they're assigned.
> Keeping your program simple and not overly reliant on interesting
> low-level techniques is certainly worth some effort.
>
> --
> J.
My System SpecsSystem Spec
Old 4 Weeks Ago   #19 (permalink)
Patrice


 
 

Re: How to Make a .NET Timer Always Fire at the Right Time

Quote:

> I have worked with setups where the server side closed the connection
> after
> just 20 seconds of inactivity, because it was more important for the
> connection to be "known good" even at short intervals than it was to be
> mindful of the bandwidth wasted this way.
>
> I'm not saying this is a good way of doing things (using short
> transmission timeouts is probably a better idea), but I don't think it's
> unusual either.
Ok thanks I was wondering (as you saw I'm not that familiar with TCP but
thought it was connection oriented and both end had to agree to close the
connection). Charles, sorry for having hijacked this part of your thread ;-)

--
Patrice

My System SpecsSystem Spec
Old 4 Weeks Ago   #20 (permalink)
DickGrier


 
 

Re: How to Make a .NET Timer Always Fire at the Right Time

Windows is non-deterministic.

As other replies indicate, you simply cannot assure yourself that this
happens -- unless you design you system such that the user (or your own
code) cannot cause trouble. The most reliable way to do this is to dedicate
a processor, another computer, to the time-critical task. It you don't
allow the user, or your own code, to use that environment for anything other
than your time critical process, you should be OK. You'd then add a
non-time critical communications method to that process that communicates
results to your "loaded" machine, where you can then allow the user -- or
your own code -- do things that otherwise mung up the system.

Dick

--
Richard Grier, Consultant, Hard & Software 12962 West Louisiana Avenue
Lakewood, CO 80228 303-986-2179 (voice) Homepage: www.hardandsoftware.net
Author of Visual Basic Programmer's Guide to Serial Communications, 4th
Edition ISBN 1-890422-28-2 (391 pages) published July 2004, Revised July
2006.

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
What can I do to make sure the time is correct on the computers on our network? Server General
Need to click twice to fire an Event .NET General
DragEnter Does Not Fire? .NET General
chocolate fire guard... Vista General
FIRE FIRE !!!!!Video Games play over second page Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46