UNC Path disconnected after 10 minutes

Posted on 2007-10-20
Last Modified: 2012-08-14
A service that uses the FileSystemWatcher monitors a UNC Path declared in the OnStart sub. After 10 minutes the UNC path seems to be disconnected.
The service was working fine for several weeks and then suddenly the UNC Path was dropping after 10 minutes from the service starting. There are no error messages, and the service continues to run after the connection is lost.

The servers are Windows 2003 Server.
Question by:hertel-dev
    LVL 28

    Expert Comment

    Set autodisconnect to a value of ffffffff.
    LVL 11

    Accepted Solution

    FileSystemWatcher will aslo stop functioning if the computer/path that is monitoring will reboot or loose network connectivity for a moment.

    A workaround is to create a timer and in a timeinterval (e.g. 1minute-60000) you stop and restart the FileSystemWatcher. That will ensure that your FileSystemWatcher will work all the time

        Private Sub TimerStartAutoWatch_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerStartAutoWatch.Tick
            Catch ex As Exception
            End Try
        End Sub

        Private Sub StartWatching()
                FileWatcherIsWorkingNow = True
                'define path
                Me.msgFileWatcher.Path = ServerWatchFolderAndPath
                Me.msgFileWatcher.Filter = "*.tmp"
                Me.msgFileWatcher.IncludeSubdirectories = True
                Me.msgFileWatcher.NotifyFilter = NotifyFilters.Size Or NotifyFilters.FileName Or NotifyFilters.DirectoryName
                ' add the handler to each event
                AddHandler msgFileWatcher.Created, AddressOf msgFileWatcher_Changed
                AddHandler msgFileWatcher.Changed, AddressOf msgFileWatcher_Changed
                'AddHandler msgFileWatcher.Renamed, AddressOf msgFileWatcher_Renamed
                'Set this property to true to start watching
                msgFileWatcher.EnableRaisingEvents = True
            Catch ex As Exception
            End Try
        End Sub

    Private Sub stopWatching()
            Dim errCounter As Long = 0
            On Error Resume Next
            'Set this property to false to STOP watching
            msgFileWatcher.EnableRaisingEvents = False
            RemoveHandler msgFileWatcher.Created, AddressOf msgFileWatcher_Changed
            RemoveHandler msgFileWatcher.Changed, AddressOf msgFileWatcher_Changed
            'RemoveHandler msgFileWatcher.Renamed, AddressOf msgFileWatcher_Renamed
            If Err.Number > 0 Then
                errCounter += 1
                If errCounter < 20 Then
                    GoTo startStoppingAgain
                End If
            End If
            errCounter = Nothing
            On Error GoTo 0
        End Sub

    I have been using this approach in my projects.

    Author Comment

    honmapog:I had already tried your suggested solution with no luck.

    AkisC: This is a good way of doing things. I implemented the timer code using a thread and put the polling routine in a loop. Like this:

    Private m_oPollingThread As New Thread( _
                New System.Threading.ThreadStart(AddressOf pollProcess))

    'placed in the sub onStart() of the service

    Private Sub pollProcess()
                    System.Threading.Thread.Sleep(2000)  ' 2 seconds      
            Catch ex As Exception
            End Try
        End Sub

    Unfortunately I still have the same problem.
    LVL 11

    Expert Comment

    You still have the same problem , so you get an error
    what does the error message say?
     Catch ex As Exception
    msgbox(ex.Tostring) '// since it is a service drop the message to a file...
            End Try
    Since the UNC Path disconects at some time of inactivity, a workaround could be to write/delete a file to (Me.Path) in a time interval of less than 10 minutes, so you trick the disconnect timer. Filewatcher is just waiting...

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    On July 14th 2015, Windows Server 2003 will become End of Support, leaving hundreds of thousands of servers around the world that still run this 12 year old operating system vulnerable and potentially out of compliance in many organisations around t…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now