简体   繁体   English

PowerShell CloseHandle on EventWaitHandle

[英]PowerShell CloseHandle on EventWaitHandle

I have two PowerShell scripts.我有两个 PowerShell 脚本。 One of them has to wait at the other at one point.其中一个必须在某个时间点在另一个等待。 Here are the relevant parts:以下是相关部分:

WaitingScript.ps1:等待脚本.ps1:

$StopEventName = 'MyEvent'

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

SignallingScript.ps1: 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.它运行良好,我只是不确定是否应该在任一脚本中调用 CloseHandle 或 Close on $StopEventObject

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请参阅Microsoft 的 WaitHandle.Close

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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