0

I have two PowerShell scripts. One of them has to wait at the other at one point. Here are the relevant parts:

WaitingScript.ps1:

$StopEventName = 'MyEvent'

function Wait-StopEvent {
    $EventResetModeManualReset = 1
    $StopEventObject = New-Object -TypeName System.Threading.EventWaitHandle -ArgumentList $false, $EventResetModeManualReset, $StopEventName
    $StopEventObject.WaitOne()
}

SignallingScript.ps1:

$StopEventName = 'MyEvent'

function Signal-StopEvent {
    $StopEventObject = [System.Threading.EventWaitHandle]::OpenExisting( $StopEventName )
    $StopEventObject.Set()
}

It works well, I'm just not sure if I should call something like CloseHandle, or Close on $StopEventObject in either script.

z32a7ul
  • 3,695
  • 3
  • 21
  • 45

1 Answers1

1

Yes - at least I don't see a reason why you should not close the handle - otherwise the resources used by the handle would not be released. See WaitHandle.Close at Microsoft

  • And should I close it in both `Wait-StopEvent` and `Signal-StopEvent`, or only in `Wait-StopEvent`, which created it? In WinAPI I would call on both sides, but the page you linked says, it will call Dispose. Is it alright if Dispose is called twice? – z32a7ul May 30 '21 at 10:13