I’m running a timer like this:
private void InitializeTimer()
{
Timer myTimer = new Timer();
myTimer.Interval = 3000;
myTimer.Enabled = true;
myTimer.Tick += new EventHandler(TimerEventProcessor);
myTimer.Start();
}
It will trigger an event every 3rd second. The event is not very heavy I think, it is reading text from a file, comparing text length to the text in a textbox and will replace the text in the box if it has more characters.
But how resource heavy is the timer?
And is it a bad idea to read text from a file every 3rd second (the file is a log file in plain text).
A Timer is fairly lightweight, though it does depend a bit on which type of timer you’re using. Having a timer fire every 3 seconds is not likely to be a big deal, though if you’re using a Windows Forms timer, make sure your Tick event handler is not doing any significant processing, as it happens in the UI thread. (For example, check the file length using
FileInfo.Lengthinstead of “reading” the file, etc)That being said, for watching for changes in a log file,
FileSystemWatcheris likely better than using a timer and re-reading the file continually. In addition to notifying you immediately when the file changes, it also will keep you from having to reading the file continually.