0

I wrote code with the WPF window to synchronize files between two folders. After clicking the Run button, the code will run and monitor changes in folder f1. If a change is detected, it will execute the condition with f2. However, after I pressed the run button, nothing happened, WPF froze and had to be turned off. Please tell me what I did wrong and how I should fix it? Sincerely thank you!

function StartWatching {
 param (
 [string]$F1s,
 [string]$F2s,
 [bool]$Recycles,
 [bool]$Backups,
 [string]$BackupPaths,
 [int]$Time
 )
 if (($Recycles -eq $true) -or ($Backups -eq $true)) {
 Check-Folders -F1 $F1s -F2 $F2s -Recycle $Recycles -Backup $Backups -BackupPath $BackupPaths
 }
 $watcher = New-Object System.IO.FileSystemWatcher
 $watcher.Path = $F1s
 $watcher.IncludeSubdirectories = $true
 $watcher.NotifyFilter = [System.IO.NotifyFilters]'FileName, DirectoryName, LastWrite, Size'
 $action = {
 param($sourceEventArgs)
 if (($Recycles -eq $true) -or ($Backups -eq $true)) {
 Check-Folders -F1 $F1s -F2 $F2s -Recycle $Recycles -Backup $Backups -BackupPath $BackupPaths
 }
 robocopy $F1s $F2s /MIR /COPYALL /Z /R:5 /W:10
 }
 $onChanged = Register-ObjectEvent -InputObject $watcher -EventName Changed -Action $action
 $onCreated = Register-ObjectEvent -InputObject $watcher -EventName Created -Action $action
 $onDeleted = Register-ObjectEvent -InputObject $watcher -EventName Deleted -Action $action
 $onRenamed = Register-ObjectEvent -InputObject $watcher -EventName Renamed -Action $action
 $watcher.EnableRaisingEvents = $true
 while ($true) {
 Start-Sleep -Seconds $Time
 }
}
$watcher = $null
$onChanged = $null
$onCreated = $null
$onDeleted = $null
$onRenamed = $null
...
$run.Add_Click({
 StartWatching -F1s $folder1.Text -F2s $folder2.Text -Recycles $Recycle.IsChecked -Backups $Backup.IsChecked -BackupPaths $BackupDir.Text -Time $rptime.Text
})
$window.ShowDialog() | Out-Null
if ($watcher -ne $null) {
 $watcher.EnableRaisingEvents = $false
 Unregister-Event -SubscriptionId $onChanged.Id
 Unregister-Event -SubscriptionId $onCreated.Id
 Unregister-Event -SubscriptionId $onDeleted.Id
 Unregister-Event -SubscriptionId $onRenamed.Id
 $watcher.Dispose()
}
asked Jun 6, 2024 at 14:00
3
  • 2
    "Please tell me what I did wrong and how I should fix it?" Please provide a minimal reproducible example (without ... in the code.) Commented Jun 6, 2024 at 14:08
  • Please try to debug you own script, using: How to Debug Scripts in Windows PowerShell ISE Commented Jun 6, 2024 at 14:21
  • 1
    Debug using Visual Studio Code. code.visualstudio.com/download It is the right price (USD 0.00). ISE is no longer under development. Commented Jun 6, 2024 at 17:32

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.