PowerShell CloseHandle on EventWaitHandle

354 views Asked by At

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.

1

There are 1 answers

1
Stephan Keil On BEST ANSWER

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