-2

I am wondering where can I find some sample code to do this in C#:

  1. A BackGroundWorker periodically (say, 4 times per second) checking a .txt file to see it one particular string, such as "I am done." get written in the file.

  2. Once the BackGroundWorker finds such a string exist in the .txt, it should trigger a event becoming true.

I am a newbie for C#, so some sample would be highly appreciated. Thanks a lot.

Luis Filipe
  • 8,488
  • 7
  • 48
  • 76
Nick X Tsui
  • 2,737
  • 6
  • 39
  • 73

3 Answers3

2

There are two basic strategies to find out if something changed. You can poll, repeatedly check if anything is different. Or you can use an event, code that runs automagically when the change occurs. Events are of course much more effective, you don't waste any time and system resources to accomplishing little to nothing. You for example never poll a button to see if the user clicked it, you use its Click event instead.

Sometimes polling is inevitable, simply because an event is not available. But there certainly is one for a change to a file, the operating system has support for it. Exposed in .NET through the FileSystemWatcher class. Strongly recommended over a Timer or a worker thread, opening and reading every line of a file to discover a "I am done" string is very expensive.

Just be careful when you try to read the file. Also a problem when you use a Timer or worker thread, but more so because FileSystemWatcher works so much better. It is pretty likely that you can't open the file yet when the Change event fires because whatever process writes the file still has a lock on it. You may well still need a Timer to try to read the file later, after it is no longer locked.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

You probably just want to use Timer. A sample code is something like this

var timer = new Timer(TimerTick, null, TimeSpan.Zero, new TimeSpan(0, 0, 0, 1));

int lastMinute = 1;

void TimerTick(object state)
{
    var minute = DateTime.Now.Minutes;
    if (minute != lastMinute && minute % 5 == 0)
    {
        lastMinute = minute;
        //Check the .txt file
    }
}

As Thorsten Dittmar pointed out, yes you probably want to use System.Timers.Timer than System.Threading.Timer. Timers.Timer is thread-safe too.

According to one of the answers from found here

The specific difference appears to be that System.Timers.Timer is geared towards multithreaded applications and is therefore thread-safe via its SynchronizationObject property, whereas System.Threading.Timer is ironically not thread-safe out-of-the-box.

I don't believe that there is a difference between the two as it pertains to how small your intervals can be.

There is also a link for a comprehensive explanation of timers.

Community
  • 1
  • 1
123 456 789 0
  • 10,565
  • 4
  • 43
  • 72
  • You could use a `System.Timers.Timer`, which is easier to start/stop/control than the `System.Threading.Timer`, but a timer is the way to go. A background worker is not suited for this task. Both a `System.Timers.Timer` and a `System.Threading.Timer` would run in the background in separate threads, too. – Thorsten Dittmar Jan 28 '14 at 16:55
1

There is already built in functionality to do this. Try using the FileWatcher class.

http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v=vs.110).aspx

Jon Raynor
  • 3,804
  • 6
  • 29
  • 43