简体   繁体   中英

PowerShell CloseHandle on EventWaitHandle

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM