How to detect a network disconnect of a share

  • Thread starter Thread starter Douglas Gallant
  • Start date Start date
D

Douglas Gallant

I have a FileSystemWatcher monitoring a UNC path in a Windows service
routine. If network connectivity is disrupted for some reason (including the
remote server being restarted), the application stops receiving FSW events.
How can I determine if I have lost connectivity and force a restart of the
service (or whatever makes sense)? I'm hoping there is some event I can hook
into but that may be optimistic. I suppose setting up a Timer and trying
some type of access to the UNC path on the timer event would work but just
seems a tad crude.

Also, how do I force a service to restart? Do I have to throw an exception
and have the service configured to auto-restart after some interval or can
this be done cleanly within the service?

Thanks,
Douglas Gallant
 
Douglas Gallant said:
I have a FileSystemWatcher monitoring a UNC path in a Windows service
routine. If network connectivity is disrupted for some reason (including the
remote server being restarted), the application stops receiving FSW events.
How can I determine if I have lost connectivity and force a restart of the
service (or whatever makes sense)? I'm hoping there is some event I can hook
into but that may be optimistic. I suppose setting up a Timer and trying
some type of access to the UNC path on the timer event would work but just
seems a tad crude.

I am trying to do the same thing -- detect when FileSystemWatcher is
going to stop working on a network share when the network disconnects.
I find that FileSystemWatcher.Error gets called when the network
disconnects and ErrorEventArgs.GetException() returns a "The specified
network name is no longer available" exception. So I watch for those.

- M
 
I'll check into that. However, what do you do when this condition occurs
relative to the FSW component? Is a simple disable/enable of events
sufficient or is something more drastic such as destroying and recreting the
FSW required?
 
Douglas Gallant said:
I'll check into that. However, what do you do when this condition occurs
relative to the FSW component? Is a simple disable/enable of events
sufficient or is something more drastic such as destroying and recreting the
FSW required?

Unfortunately, I don't know of a good solution. This is for an
internal tool so notification of the error is more important to me
than automatic recovery. In my tool, subsequent user action causes me
to recreate the FSW and it starts working again.

If the network is truly disconnected, you won't know when to recreate
the FSW. This leads to some kind of polling for success, which then
leads to the previous issues of missing events. Ugly.

Finally, I left my tool running last night monitoring a local network
share, and it mysteriously stopped monitoring events and apparently
FileSystemWatcher.Error didn't get called. So there seems to be
further unresolved issues when monitoring over the network.

- M
 
Back
Top