PC Review


Reply
Thread Tools Rate Thread

C# Timer control

 
 
Frank Mills
Guest
Posts: n/a
 
      9th May 2005

Hi,

I have a timer control which writes to a file every 5 seconds but the
application closes down before it gets to the second firing of the
timer. How can i get the application to stay alive until the next timer
interval comes around?

Thanks


*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
 
 
 
TT \(Tom Tempelaere\)
Guest
Posts: n/a
 
      9th May 2005
Hi Frank,

"Frank Mills" <(E-Mail Removed)> schreef in bericht
news:%(E-Mail Removed)...
>
> Hi,
>
> I have a timer control which writes to a file every 5 seconds but the
> application closes down before it gets to the second firing of the
> timer. How can i get the application to stay alive until the next timer
> interval comes around?
>
> Thanks
> *** Sent via Developersdex http://www.developersdex.com ***


In your timer code, call a method that sets an event if the application is
exiting. Have the UI thread wait for the event before exiting. You can use
ManualResetEvent class as the event class. You can only do this when the
timer is not invoked (synchronized) on the UI thread, so the timer should
execute on another thread. I'll sketch the outline:

<code>
private bool _exitingApp = false;
private object _closingLock = new object();

private ManualResetEvent timerTick = new ManualResetEvent( false );

private bool ExitingApp {
get {
lock( _closingLock )
return _exitingApp;
}
set {
lock( _closingLock )
_exitingApp = value;
}
}

// Timer handler outline, should not be invoked (synchronized) on UI thread
private void TimerCode()
{
// do timer things

ReportTimerTick();
}

private void ReportTimerTick()
{
if( ExitingApp )
timerTick.Set();
}

// form's closing handler
private void OnFormClosing( object sender, FormClosingEventArgs e )
{
ExitingApp = true;

timerTick.WaitOne();
}
</code>

Hope this helps,
Tom T.


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Timer Control =?Utf-8?B?VW1lc2huYXRo?= Microsoft ASP .NET 2 16th Oct 2006 10:54 AM
timer control Gurpreet Microsoft Access Queries 1 29th Jul 2006 07:39 AM
Timer Control =?Utf-8?B?TmVv?= Microsoft Dot NET 1 21st Mar 2006 12:40 PM
Timer Control source Microsoft C# .NET 2 23rd Aug 2004 06:14 PM
Timer control vs Timer class VJ Microsoft Dot NET Framework 2 18th Apr 2004 05:55 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:32 PM.