FileSystemWatcher获取无效的内存访问错误

时间:2017-05-03 21:43:59

标签: .net filesystemwatcher

我对FileSystemWatcher有一个奇怪的问题。

它获得了对内存位置的无效访问" Win32Exception传递给它的Error事件处理程序。此异常没有堆栈跟踪,因此我无法查看错误发生的位置,但它必须是观察程序本身的一部分。 当发生这种情况时,观察者停止响应,在重新启动之前我不会再接收任何事件。

我能够通过Google找到的唯一接近此错误的是它是否内存不足或者更改发生的速度超过了处理速度。但是这在程序启动后相当快地发生,并且内存不是问题,并且在错误事件之前发生的少数事件被快速处理。

我不知道这是否相关,但我通常在观看UNC路径或映射的网络驱动器。 编辑:我已经确认这只是 网络共享的问题。它在本地驱动器上运行良好。

此代码已存在多年,几个月前刚刚开始讨论此问题。我不会排除基础架构更改或Windows更新导致它,但我正在寻找方法来诊断导致它的原因。

编辑:以下是它所做的精简版(VB.NET)

Private Class Watcher
    Implements IDisposable

    Private WithEvents _FileSystemWatcher As IO.FileSystemWatcher

    Sub New(directory As String)
        _FileSystemWatcher = New IO.FileSystemWatcher
        _FileSystemWatcher.Path = directory
        _FileSystemWatcher.IncludeSubdirectories = False
    End Sub

    Private Sub _FileSystemWatcher_FileSystemEvent(source As Object, e As FileSystemEventArgs) Handles _FileSystemWatcher.Created, _FileSystemWatcher.Changed, _FileSystemWatcher.Deleted
        ' removed for brevity - For the purposes of this problem, it 
        ' is irrelevant.  We get the error even if there are no events here.
    End Sub

    Private Sub _FileSystemWatcher_Error(ByVal source As Object, ByVal e As ErrorEventArgs) Handles _FileSystemWatcher.Error
        Dim ex = e.GetException()
        If ex IsNot Nothing Then
            Dim win32ex = TryCast(ex, System.ComponentModel.Win32Exception)
            ' At this point, win32ex.NativeErrorCode will be 998 for the
            ' Invalid Memory Access
        End If
    End Sub

    ' Dispose implementation removed for brevity on StackOverflow post,
    ' but it disposes the _FileSystemWatcher.

End Class

0 个答案:

没有答案