FileSystemWatcher and multiple, fast changes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    FileSystemWatcher and multiple, fast changes

    I have a FileSystemWatch er watching a directory where I will add and update
    files. When it detects a change, I search through all the files and update
    my list of files in the UI. This works fine.

    Now, my other application that is writing the files that are being watched,
    does something like this:
    1) Creates a file, a.txt
    2) wants to name it b.txt but checks first if b.txt exists, if it does, it
    delete it
    3) moves a.txt to b.txt (rename)

    what happens is that when b.txt is deleted, my other app that is watching
    that folder starts processing the files in the directory(Creat ing FileInfo
    objects, etc) In that processing, it's looping through a collection of
    files that WERE there when it was initiated.

    Then the call to Move() happens and now the FileSystemWatch er app has
    invalid data as it hasn't returned fast enough to finish before the move()
    call happened.

    I hope this makes sense to someone. Basically, application A is deleting
    resources that Application B is still working with.

    Do I just catch the FileNotFoundExc eption and deal with it? Any design
    suggestions to work with this scenario?




  • Steve

    #2
    Re: FileSystemWatch er and multiple, fast changes


    "Steve" <sss@sss.com> wrote in message
    news:%23LJgV00Y GHA.3832@TK2MSF TNGP04.phx.gbl. ..[color=blue]
    >I have a FileSystemWatch er watching a directory where I will add and update
    >files. When it detects a change, I search through all the files and update
    >my list of files in the UI. This works fine.
    >
    > Now, my other application that is writing the files that are being
    > watched, does something like this:
    > 1) Creates a file, a.txt
    > 2) wants to name it b.txt but checks first if b.txt exists, if it does,
    > it delete it
    > 3) moves a.txt to b.txt (rename)
    >
    > what happens is that when b.txt is deleted, my other app that is watching
    > that folder starts processing the files in the directory(Creat ing FileInfo
    > objects, etc) In that processing, it's looping through a collection of
    > files that WERE there when it was initiated.
    >
    > Then the call to Move() happens and now the FileSystemWatch er app has
    > invalid data as it hasn't returned fast enough to finish before the move()
    > call happened.
    >
    > I hope this makes sense to someone. Basically, application A is deleting
    > resources that Application B is still working with.
    >
    > Do I just catch the FileNotFoundExc eption and deal with it? Any design
    > suggestions to work with this scenario?
    >[/color]

    This seems like I hack, but I added a bool flag to determine if I'm working
    with files or not, something like this:
    <code>
    private void OnWatchedDirect oryContentsChan ged(object source,
    FileSystemEvent Args e)
    {
    if (_workingWithA4 3Files == false)
    {
    LoadAvailableFi les();
    }
    }
    </code>

    Inside LoadAvailableFi les() I set _workingWithA43 Files true at the start
    and false when I'm done.
    This isn't working consistently though...


    Comment

    • Frank Rizzo

      #3
      Re: FileSystemWatch er and multiple, fast changes

      Steve,

      Back in the VB2 days (long before FileSystemWatch er) I used a semaphore
      approach. Basically both applications knew that SomeFile.sem was the
      semaphore file. The procedure to do work on a certain file was like this:

      1. Does SomeFile.sem exist? If yes, the other application is doing
      work. Try again later. If file doesn't exist, go to step 2.
      2. Create SomeFile.sem. On success go to step 3. On failure, start over.
      3. Do work.
      4. Delete the semaphore file.

      This method worked flawlessly. Never had any problems with it.
      These days, I would use a system semaphore via P/Invoke (unless .net2.0
      has something I don't know about), but back then I didn't know how to
      access a semaphore from vb2.

      As far as your current situation, handling FileNotFoundExc eption is a
      good first step, but given that both applications access resources in a
      non-managed way, you are basically asking for trouble and working
      without a net.


      Regards

      Steve wrote:[color=blue]
      > "Steve" <sss@sss.com> wrote in message
      > news:%23LJgV00Y GHA.3832@TK2MSF TNGP04.phx.gbl. ..[color=green]
      >> I have a FileSystemWatch er watching a directory where I will add and update
      >> files. When it detects a change, I search through all the files and update
      >> my list of files in the UI. This works fine.
      >>
      >> Now, my other application that is writing the files that are being
      >> watched, does something like this:
      >> 1) Creates a file, a.txt
      >> 2) wants to name it b.txt but checks first if b.txt exists, if it does,
      >> it delete it
      >> 3) moves a.txt to b.txt (rename)
      >>
      >> what happens is that when b.txt is deleted, my other app that is watching
      >> that folder starts processing the files in the directory(Creat ing FileInfo
      >> objects, etc) In that processing, it's looping through a collection of
      >> files that WERE there when it was initiated.
      >>
      >> Then the call to Move() happens and now the FileSystemWatch er app has
      >> invalid data as it hasn't returned fast enough to finish before the move()
      >> call happened.
      >>
      >> I hope this makes sense to someone. Basically, application A is deleting
      >> resources that Application B is still working with.
      >>
      >> Do I just catch the FileNotFoundExc eption and deal with it? Any design
      >> suggestions to work with this scenario?
      >>[/color]
      >
      > This seems like I hack, but I added a bool flag to determine if I'm working
      > with files or not, something like this:
      > <code>
      > private void OnWatchedDirect oryContentsChan ged(object source,
      > FileSystemEvent Args e)
      > {
      > if (_workingWithA4 3Files == false)
      > {
      > LoadAvailableFi les();
      > }
      > }
      > </code>
      >
      > Inside LoadAvailableFi les() I set _workingWithA43 Files true at the start
      > and false when I'm done.
      > This isn't working consistently though...
      >
      >[/color]

      Comment

      Working...