Re: Waiting for a timer event in a console application?

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 11/23/04


Date: Tue, 23 Nov 2004 08:56:13 -0000

Kurt <kurbylogic@hotmail.com> wrote:
> Rather then Sleep, wake, check enabled, Sleep, wake, check enabled,
> etc..
> You can use a ManualResetEvent to signal the main thread when you have
> disabled the timer and thread will only need to wake when the event is
> signaled
>
> class Foo
> {
> ManualResetEvent mExitEvent = new ManualResetEvent(false);
> Timer mTimer = new Timer(1000);
> ...
> void Run()
> {
> ...
> Timer.Start();
> mExitEvent.WaitOne();
> }
> void TimerEvent(object source, ElapsedEventArgs e)
> {
> ...
> mTimer.Stop();
> mExitEvent.Set();
> }
> }

Alternatively, just use Monitor.Wait and Monitor.Pulse.

-- 
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Relevant Pages

  • [RFC] sleepy linux
    ... Download this big chunk in mozilla, then go to sleep ... but wake me up in 8:30 with mp3 player ... +void detect_idle; ... static int enter_state ...
    (Linux-Kernel)
  • Re: Who needs AppleTV?
    ... I said "about one second" long before you started to drag up specs of unrelated hard drives. ... "Are you actually trying to claim that a Mac laptop won't wake ... from sleep in about a second?" ...     in a second. ...
    (comp.sys.mac.advocacy)
  • Re: Off switch blues
    ... "It" is the operating system. ... Here's what the Apple documentation says: "To wake your computer from ... sleep, press a key on the keyboard, or click the mouse. ... requires this), the problem is that it STAYS awake, even though the user ...
    (comp.sys.mac.system)
  • Re: Lady DArbanville Lyrics (aka nsa Timisoara+Moldova Scenario second location) Re: remember phoeni
    ... My Lady d'Arbanville why do you sleep so still? ... I'll wake you tomorrow ... This rose will never die this rose will never die. ... hei joben, ...
    (soc.culture.romanian)
  • =?utf-8?Q?Re:_Limited_connectivity_for_my_?= =?utf-8?Q?Internet_connection_=E2=80=93Sleep_mo
    ... It fixes some network wake from sleep problems. ... I can see there's a link bewteen the computer and the "Internet modem" under Network Map. ... But that very same map has a red cross between the "Internet modem" and the Internet earth globe. ...
    (microsoft.public.windows.vista.networking_sharing)