Right now, I am attempting to manage boolean flags via a global hashtable:
# Context: System.IO.FileSystemWatcher
# File Event: 'Changed'
$global:sourceId2Flag = @{}
...
$objectEventArgs = @{
...
Action = {
$nap = 1.0 / (Get-Random -Minimum 2 -Maximum 10)
sleep $nap
Write-Verbose $nap
if ($global:sourceId2Flag[$event.MessageData.sourceId] -eq 0){
$global:sourceId2Flag[$event.MessageData.sourceId]++
Write-Verbose "Value was: $global:sourceId2Flag[$event.MessageData.sourceId]"
}
}
...
}
Register-ObjectEvent @objectEventArgs
And the main problem here is that I still generate ~3 (say, 2-6 events, depending on application that changes the file) events when the file is changed. It seems as if the hashtable is memcopied into the action context and so the gating does not work.
How would I properly set the hashtable flag so that other events do not get processed until I reset the hashtable flags to 0?
Alternatively, is there another way to solve this problem?