Re: Waiting for a timer event in a console application?
From: Dan Bass (danielbass)
Date: 11/23/04
- Next message: Chua Wen Ching: "RE: Validating XML Against a XSD Schema"
- Previous message: Nick Malik: "Re: OO Analysis"
- In reply to: Kurt: "Re: Waiting for a timer event in a console application?"
- Next in thread: Jon Skeet [C# MVP]: "Re: Waiting for a timer event in a console application?"
- Messages sorted by: [ date ] [ thread ]
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.
- Next message: Chua Wen Ching: "RE: Validating XML Against a XSD Schema"
- Previous message: Nick Malik: "Re: OO Analysis"
- In reply to: Kurt: "Re: Waiting for a timer event in a console application?"
- Next in thread: Jon Skeet [C# MVP]: "Re: Waiting for a timer event in a console application?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|