【问题标题】:Handling connection loss on FileSystemWatcher处理 FileSystemWatcher 上的连接丢失
【发布时间】:2014-02-17 12:14:20
【问题描述】:

我有一个错误处理程序,因此当网络连接断开时,它会不断检查连接是否已恢复,然后再次启动FileSystemWatcher。为此,我目前运行以下代码:

Sub errhandler(ByVal source As Object, ByVal e As  _
                    System.IO.ErrorEventArgs)

    Me.NotifyIcon1.BalloonTipIcon = ToolTipIcon.Error
    Me.NotifyIcon1.BalloonTipText = "Connection to the folder has been lost"
    Me.NotifyIcon1.BalloonTipTitle = "Connection Lost"
    Me.NotifyIcon1.ShowBalloonTip(6000)

    Do Until Dir("\\My\Path", vbDirectory) <> vbNullString
        Application.DoEvents()
    Loop
    watchFolder = Nothing
    Me.NotifyIcon1.BalloonTipIcon = ToolTipIcon.Info
    Me.NotifyIcon1.BalloonTipText = "Attempting Reconnection"
    Me.NotifyIcon1.BalloonTipTitle = "Attempting Reconnection to folder"
    Me.NotifyIcon1.ShowBalloonTip(6000)
    checkItem()
End Sub

我希望Do Until... Loop 能够检查连接是否再次正常工作,但这似乎不起作用,因此它不会继续执行其余代码(第一个工具提示显示,所以我知道这不是我的错误处理程序的问题)

这可能不是检查已恢复连接的最佳方法,但我真的不明白为什么它不起作用。如果肯定不会,有人可以解释为什么或如果应该,我在这里错过了什么吗?另请注意,我对 VB.Net 还很陌生 :)

此外,如果您需要,checkItem 正在运行以下过程(加上一些未在此处显示的日志记录)

watchFolder = New System.IO.FileSystemWatcher()
watchFolder.Path = "\\My\Path\"
watchFolder.IncludeSubdirectories = True
watchFolder.EnableRaisingEvents = True
watchFolder.NotifyFilter = IO.NotifyFilters.DirectoryName
watchFolder.NotifyFilter = watchFolder.NotifyFilter Or IO.NotifyFilters.FileName
watchFolder.NotifyFilter = watchFolder.NotifyFilter Or IO.NotifyFilters.Attributes
AddHandler watchFolder.Changed, AddressOf logchange
AddHandler watchFolder.Created, AddressOf logchange
AddHandler watchFolder.Deleted, AddressOf logchange
AddHandler watchFolder.Error, AddressOf errhandler
AddHandler watchFolder.Renamed, AddressOf logrename

【问题讨论】:

    标签: vb.net filesystemwatcher


    【解决方案1】:

    发现问题。在Dir 部分中,我的路径末尾缺少一个“\”。现在是

    Do Until Dir("\\My\Path\", vbDirectory) <> vbNullString
        Application.DoEvents()
    Loop
    

    效果很好

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      • 2018-04-29
      • 2015-01-14
      • 1970-01-01
      • 2018-01-30
      • 2020-10-26
      相关资源
      最近更新 更多