Threading and the FileSystemWatcher WatchForChanges method 
Author Message
 Threading and the FileSystemWatcher WatchForChanges method

I'm trying to create a service that uses the FileSystemWatcher to monitor a
folder and all its subfolders.  The catch is, I need to run multiple
instances of the service so that I can monitor multiple folders (some local,
some remote).  I like the idea of using the WaitForChanged method so that I
re-gain control after each timeout for a few reasons:  One - When an event
is fired and I can not pick up the file for some reason (still in use, etc.)
I mark it in a log and try it again on the next timeout (I don't have to
wait for another event to fire).  Two - I can watch for fewer events
(typically just the "created" event).  Three - When files exist in the
folder tree before the service starts they will process on the first timeout
not the first event.

Problem:
When I start the second instance of the service, "WatchForChanges" seems to
fire the first time (I can write to a text file from inside the sub) but
then it just goes off into space...  The next thread call never comes
back...  The service responses to any stop event I send it, but it never
starts looping thru the WatchForChanges sub.  The first instance continues
to work without (any known) issues.  I know this is threading problem, I'm
just not sure how to go about solving it!

Protected Overrides Sub OnStart()

 <-- omitted for clarity -->

 m_oFSW = New FileSystemWatcher
 m_oFSW.Path = sDirectory
 m_oFSW.Filter = "*.*"
 m_oFSW.IncludeSubdirectories = True

 ' start waiting...
 Dim thrThread As New Thread(AddressOf WatchForChanges)
 thrThread.Start()

 <-- omitted for clarity -->

End Sub

Protected Sub WatchForChanges()

 Dim fswResult As New WaitForChangedResult

 fswResult = m_oFSW.WaitForChanged(WatcherChangeTypes.Created, m_iTimeout)

 If Not m_bStopping Then

  <-- process file(s) code omitted for clarity -->

  ' start waiting again...
  Dim thrThread As New Thread(AddressOf WatchForChanges)
  thrThread.Start()

 End If

End Sub

Protected Overrides Sub OnStop()

 m_bStopping = True

End Sub

 - Paul



Sat, 26 Nov 2005 05:03:15 GMT  
 Threading and the FileSystemWatcher WatchForChanges method
Yikes!

I commented out the call to the class that contains the processing code and
the second instance started working as expected!  I guess its not a
threading issue... At least not with the FileSystemWatcher and my recursive
calls to the "WatchForChanges" sub.  Now I have to figure out why the calls
to processing class do not come back in the second instance...

 - Paul



Quote:
> I'm trying to create a service that uses the FileSystemWatcher to monitor
a
> folder and all its subfolders.  The catch is, I need to run multiple
> instances of the service so that I can monitor multiple folders (some
local,
> some remote).  I like the idea of using the WaitForChanged method so that
I
> re-gain control after each timeout for a few reasons:  One - When an event
> is fired and I can not pick up the file for some reason (still in use,
etc.)
> I mark it in a log and try it again on the next timeout (I don't have to
> wait for another event to fire).  Two - I can watch for fewer events
> (typically just the "created" event).  Three - When files exist in the
> folder tree before the service starts they will process on the first
timeout
> not the first event.

> Problem:
> When I start the second instance of the service, "WatchForChanges" seems
to
> fire the first time (I can write to a text file from inside the sub) but
> then it just goes off into space...  The next thread call never comes
> back...  The service responses to any stop event I send it, but it never
> starts looping thru the WatchForChanges sub.  The first instance continues
> to work without (any known) issues.  I know this is threading problem, I'm
> just not sure how to go about solving it!

> Protected Overrides Sub OnStart()

>  <-- omitted for clarity -->

>  m_oFSW = New FileSystemWatcher
>  m_oFSW.Path = sDirectory
>  m_oFSW.Filter = "*.*"
>  m_oFSW.IncludeSubdirectories = True

>  ' start waiting...
>  Dim thrThread As New Thread(AddressOf WatchForChanges)
>  thrThread.Start()

>  <-- omitted for clarity -->

> End Sub

> Protected Sub WatchForChanges()

>  Dim fswResult As New WaitForChangedResult

>  fswResult = m_oFSW.WaitForChanged(WatcherChangeTypes.Created, m_iTimeout)

>  If Not m_bStopping Then

>   <-- process file(s) code omitted for clarity -->

>   ' start waiting again...
>   Dim thrThread As New Thread(AddressOf WatchForChanges)
>   thrThread.Start()

>  End If

> End Sub

> Protected Overrides Sub OnStop()

>  m_bStopping = True

> End Sub

>  - Paul



Sat, 26 Nov 2005 05:28:48 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. calling a method on a worker thread

2. threads calling static methods

3. Aborting Method Call using Threads?

4. Seek Method Not Working (new thread)

5. Class method in a thread

6. Safe threading by delivering EVENTS to UI THREAD

7. Threading problem --> threads die

8. Thread error without threads

9. threading - Multiple threads returning values at same time

10. Opening a form in a new thread, appart from the Apps main thread

11. Will child-thread end when main thread end?

12. thread spawning a thread?

 

 
Powered by phpBB® Forum Software