0

This is in C#.

  • There is a log file (log.txt), which keeps updating during the processing. When the process is done, the last line gets written in the end of the log file is "Process finished!". No exceptions.

  • I want to constantly read the file, perhaps four times per second, to see if the string has shown up, such that I can know when the process is finished.

  • I am wondering, what is the best way (easy, light and reliable) to do it? Timer? FileWatcherSyetem? Something else?

Thanks a lot!

Nick X Tsui
  • 2,737
  • 6
  • 39
  • 73
  • See: http://stackoverflow.com/questions/3791103/c-sharp-continuously-read-file – C4F Jan 23 '14 at 15:21
  • @TheC4Fox If I understand correctly, that assumes the latest written information in a file is always arranged in the tail. Is that so? – Nick X Tsui Jan 23 '14 at 15:31
  • The particular function he wrote, yes. The important thing is that you open a FileStream with Fileshare.ReadWrite since you're sharing it with another process. You can modify that code to suit your needs of course. – C4F Jan 23 '14 at 15:35
  • OK Thanks. I am also waiting for the opinions on the timer/fileWatcher part. – Nick X Tsui Jan 23 '14 at 15:41
  • FileWatcher will notify you when the file in the directory changes. That means only when written and "LastWrite" changes. If at "Process finished!" the file is written/closed, then this will fire. If "Process finished!" happens multiple times before the file is closed you probably want to start a thread up and poll at whatever interval you find appropriate. – C4F Jan 23 '14 at 15:46
  • Did you get something working? – C4F Jan 24 '14 at 13:19
  • @TheC4Fox I used a timer, periodically reading the log.txt file, and this is wrapped inside an assembly. However, the main application did not catch the event of finish writing of the log.txt file. So I am still working on that. – Nick X Tsui Jan 24 '14 at 14:52

1 Answers1

0

In the assembly, I used a timer to periodically check the .txt file status, something like this:

System.Timers.Timer _logFileCheckTimer;

    public FijiLauncherControl()

    {        // timer set up
                    _logFileCheckTimer = new System.Timers.Timer(250);  
                    _logFileCheckTimer.Enabled = true;
                    _logFileCheckTimer.Elapsed += new System.Timers.ElapsedEventHandler(_logFileCheckTimer_Elapsed);
                   _logFileCheckTimer.Start();  // start the timer

     }    

     void _logFileCheckTimer_Elapsed(object sender, EventArgs e)
        {
            if (_processOn && IsLogOn)
            {
                try
                {
                    _processFinished = CheckStatuts();  // checking file status

                    if (_processFinished) // fire event if checking status returns true
                    {
                        OnIjmFinished(EventArgs.Empty);
                        _processOn = false;
                        _logFileCheckTimer.Stop();
                    }
                }
                catch (Exception ex)
                {

                }
            }
        }

In the aseembly. setup a event in the dll that will be triggered if the _processFinished = CheckStatuts(); returns true:

// event delegate public delegate void IjmFinishedEventHandler(object sender, EventArgs e); public event IjmFinishedEventHandler IjmFinished; // event handler

   protected virtual void OnIjmFinished(EventArgs e)
   {
      if (IjmFinished != null)   // trigger event
                IjmFinished(this, e);          
   }

In the main application, there should be event receiver, something like this:

private FijiLauncherControl _fl; // the object of your assembly

  _fl.IjmFinished += new IjmFinishedEventHandler(_fl_IjmFinished); // event from the assembly should trigger an event handler

   void _fl_IjmFinished(object sender, EventArgs e)   // event handler
        {
            _vm.IjmFinished = true;   // a boolean var in viewmodel in main app set to be true once the event from the assembly is triggered

            //throw new NotImplementedException();
        }

That should do the job.

Nick X Tsui
  • 2,737
  • 6
  • 39
  • 73