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

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Dan Bass (danielbass)
Date: 11/23/04


Date: Tue, 23 Nov 2004 07:44:45 -0000

That's pretty cool!

"Kurt" <kurbylogic@hotmail.com> wrote in message
news:275dd576.0411221614.55feb397@posting.google.com...
> 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();
> }
> }
>
> - Kurt
>
> "Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in
> message news:<Oqk2xJK0EHA.3044@TK2MSFTNGP14.phx.gbl>...
>> Just insert a System.Threading.Thread.Sleep ( 1000 ) into your loop.
>> The integer is milliseconds so adjust the number to suit your app...
>>
>> presumably timer.Enabled is changed by the event handler? Otherwise
>> you'll
>> never leave the loop!
>>
>>
>> "avivgur" <avivgur@gmail.com> wrote in message
>> news:2c96153.0411220429.4e78f3dd@posting.google.com...
>> > Hello,
>> > I have devised a console application that uses a System.Timers.Timer
>> > and its Elapsed event.
>> > The problem is that I want the program to continue to run so that each
>> > time the event is raised, the handler will be executed.
>> > I tried using a while(timer.Enabled) {} loop but it made the process
>> > use 90% cpu in the task manager. What is the correct way to wait for
>> > an event and keep the program alive in a console application?
>> > Thanks,
>> > Aviv.



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)
  • =?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)