如何停止Filesystem Watcher? [英] How to Stop Filesystem Watcher?

查看:99
本文介绍了如何停止Filesystem Watcher?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我已经成功完成了使用文件系统监视器监视计算机上所有硬盘驱动器的任务,但现在问题是它不会停止或退出我的每个循环使用传统方法filesystemwatcher1.enableraisingevents = false



这里是完整的代码



Ok I have successfully achieved my task of monitoring all hard drives on my computer with filesystem watcher but now the problem is that it will not stop or exit my for each loop with the conventional method "filesystemwatcher1.enableraisingevents = false"

here is the full code

 Imports System.IO
Public Class Form1
    Private Sub OnChanged(ByVal sender As Object, ByVal e As FileSystemEventArgs)
        ' Specify what is done when a file is changed, created, or deleted.
        ListBox1.Items.Add(e.FullPath & " " & e.ChangeType)
        Label6.Text = (e.FullPath & " " & e.ChangeType)
        Label1.Text += 1
    End Sub
    Private Sub OnRenamed(ByVal sender As Object, ByVal e As RenamedEventArgs)
        ' Specify what is done when a file is renamed.
        ListBox1.Items.Add(e.FullPath & " " & e.ChangeType)
        Label6.Text = (e.FullPath & " " & e.ChangeType)
        Label1.Text += 1
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CheckForIllegalCrossThreadCalls = False
        FSW()
    End Sub
    Public Sub FSW()
        Dim drives As String() = Environment.GetLogicalDrives()
        For Each strDrive As String In drives
            'Check if the drive is ready to be used 
            Dim df As New DriveInfo(strDrive)
            If Not df.IsReady Then
                Continue For
            End If
            Dim _watcher As New FileSystemWatcher()
            _watcher.IncludeSubdirectories = True
            _watcher.Path = strDrive
            _watcher.NotifyFilter = NotifyFilters.Attributes Or NotifyFilters.CreationTime Or NotifyFilters.DirectoryName Or NotifyFilters.FileName _
            Or NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.Security Or NotifyFilters.Size
            AddHandler _watcher.Changed, AddressOf OnChanged
            AddHandler _watcher.Created, AddressOf OnChanged
            AddHandler _watcher.Deleted, AddressOf OnChanged
            AddHandler _watcher.Renamed, AddressOf OnRenamed
                _watcher.EnableRaisingEvents = True
        Next
    End Sub
End Class







如何重新排列这个以便我可以启动和停止文件系统观察器?



提前谢谢!!




How can i rearrange this so that I can start and stop the filesystem watcher?

thank you in advance!!

推荐答案

您可以使用至少两种方法中的一种,具有不同程度的亮度。 />


  • 当您将一些命名方法添加到 FileSystemWatcher 的事件实例的调用列表中时,也可以删除它们。在VB.NET中,使用 AddHandler 将处理程序添加到某个事件实例的调用列表中,使用 RemoveHandler 删除(与C#''+ =''和'' - =''相同。请参阅,例如:

    http://www.thescarms.com/dotnet/EventHandler。 aspx [ ^ ]。
  • 使用处理程序方法,因为它们已经定义。在声明类(表单,在您的情况下)中添加一个布尔字段,例如 allowHandling 。将它用作标志:在每个处理程序方法中添加对此标志的检查;如果标志为假,则不执行任何操作。这样,将值false赋给此标志将暂停处理,true将恢复它。简单,不是吗?
You can use one of at least two approaches, of different degree of "lightness".

  • As you add some named methods to the invocation lists of event instances of FileSystemWatcher, you can also remove them. In VB.NET, a handler is added to an invocation list of some event instance using AddHandler, removed using RemoveHandler (same as C# ''+='' and ''-=''). Please see, for example:
    http://www.thescarms.com/dotnet/EventHandler.aspx[^].
  • Use your handler method as they are already defined. Add a Boolean field in your declaring class (form, in your case), such as allowHandling. Use it as a flag: add a check of this flag in every handler method; if the flag is false, do nothing. This way, assigning the value false to this flag will pause handling, true will resume it. Simple, isn''t it?


这篇关于如何停止Filesystem Watcher?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆