在操作期间暂停 FileSystemWatcher

Pause FileSystemWatcher during action

我有这个 PowerShell 脚本,它可以监视文件是否已保存在文件夹中,然后调用中继来控制交通灯。找到新图像后,交通灯变为红色,等待 60 秒,然后变回绿色。

正如预期的那样 - 在这 60 秒内,当一个新文件被添加到此文件夹时,它会进入队列 - 所以当灯变回绿色时,它们会切换回红色并持续 60 秒。

创建文件时停止 filesystemwatcher 并重新启动它的最干净的方法是什么? 停止 FileSystemWatcher 没问题,当我处理 $FileSystemWatcher - 但重新启动它不起作用。另外,如果可能的话,我希望代码尽可能不冗余,而不是在我的 Action 中复制所有 FSW 代码(如果可能的话)

Write-Host "Auto Relay Change On Detection"
Add-Type -AssemblyName PresentationCore,PresentationFramework
$settingsFile = "$PSScriptRoot\settings.json"
$j = Get-Content -Raw -Path $settingsFile | ConvertFrom-Json
$PathToMonitor = $j.imageFolder

If(!(test-path $PathToMonitor))
{
      New-Item -ItemType Directory -Force -Path $PathToMonitor
}

$FileSystemWatcher = New-Object System.IO.FileSystemWatcher
$FileSystemWatcher.Path  = $PathToMonitor
$FileSystemWatcher.IncludeSubdirectories = $true

# make sure the watcher emits events
$FileSystemWatcher.EnableRaisingEvents = $true

# define the code that should execute when a file change is detected
$Action = {
    $details = $event.SourceEventArgs
    $Name = $details.Name
    $FullPath = $details.FullPath
    $OldFullPath = $details.OldFullPath
    $OldName = $details.OldName
    $ChangeType = $details.ChangeType
    $Timestamp = $event.TimeGenerated
    $text = "{0} was {1} at {2}" -f $FullPath, $ChangeType, $Timestamp

    # you can also execute code based on change type here
    switch ($ChangeType)
    {
        'Changed' { "CHANGE" }
        'Created' { "CREATED"
            $text = "File {0} was created." -f $Name
            Write-Host $text -ForegroundColor Yellow  
            Write-Host  "Relay = detected at $(Get-Date)" -ForegroundColor Yellow 
            #DO API CALL
            $timeout = $j.timer
            Start-Sleep -s $timeout
            #DO API CALL
            Write-Host  "Relay = default at $(Get-Date)" -ForegroundColor Yellow 
        }
        'Deleted' { "DELETED" }
        'Renamed' { "RENAMED" }
        default { Write-Host $_ -ForegroundColor Red -BackgroundColor White }
    }
}

# add event handlers
$handlers = . {
    Register-ObjectEvent -InputObject $FileSystemWatcher -EventName Created -Action $Action -SourceIdentifier FSCreate
}

try
{
    do
    {
        Wait-Event -Timeout 1
        ##Write-Host "." -NoNewline
        
    } while ($true)
}
finally
{
    Unregister-Event -SourceIdentifier FSCreate
    $handlers | Remove-Job
    $FileSystemWatcher.EnableRaisingEvents = $false
    $FileSystemWatcher.Dispose()
    "Event Handler disabled."
}
$Action = {
    try {
        $FileSystemWatcher.EnableRaisingEvents = $false
        $details = $event.SourceEventArgs
        $Name = $details.Name
        $FullPath = $details.FullPath
        $OldFullPath = $details.OldFullPath
        $OldName = $details.OldName
        $ChangeType = $details.ChangeType
        $Timestamp = $event.TimeGenerated
        $text = "{0} was {1} at {2}" -f $FullPath, $ChangeType, $Timestamp

        # you can also execute code based on change type here
        switch ($ChangeType)
        {
            'Changed' { "CHANGE" }
            'Created' { "CREATED"
                $text = "File {0} was created." -f $Name
                Write-Host $text -ForegroundColor Yellow  
                Write-Host  "Relay = detected at $(Get-Date)" -ForegroundColor Yellow 
                #DO API CALL
                $timeout = $j.timer
                Start-Sleep -s $timeout
                #DO API CALL
                Write-Host  "Relay = default at $(Get-Date)" -ForegroundColor Yellow 
            }
            'Deleted' { "DELETED" }
            'Renamed' { "RENAMED" }
            default { Write-Host $_ -ForegroundColor Red -BackgroundColor White }
        }
    } finally {
        $FileSystemWatcher.EnableRaisingEvents = $true
    }
}