Wait-Event
Waits until a particular event is raised before continuing to run.
Sintaxis
Default (Es el valor predeterminado).
Wait-Event
[[-SourceIdentifier] <String>]
[-Timeout <Int32>]
[<CommonParameters>]
Description
The Wait-Event
cmdlet suspends execution of a script or function until a particular event is
raised. Execution resumes when the event is detected. To cancel the wait, press
CTRL+C.
This feature provides an alternative to polling for an event. It also allows you to determine the response to an event in two different ways:
- using the Action parameter of the event subscription
- waiting for an event to return and then respond with an action
Ejemplos
Example 1: Wait for the next event
This example waits for the next event that is raised.
Wait-Event
Example 2: Wait for an event with a specified source identifier
This example waits for the next event that is raised and that has a source identifier of ProcessStarted.
Wait-Event -SourceIdentifier "ProcessStarted"
Example 3: Wait for a timer elapsed event
This example uses the Wait-Event
cmdlet to wait for a timer event on a timer that is set for 2000 milliseconds.
$Timer = New-Object Timers.Timer
$objectEventArgs = @{
InputObject = $Timer
EventName = 'Elapsed'
SourceIdentifier = 'Timer.Elapsed'
}
Register-ObjectEvent @objectEventArgs
$Timer.Interval = 2000
$Timer.Autoreset = $false
$Timer.Enabled = $true
Wait-Event Timer.Elapsed
ComputerName :
RunspaceId : bb560b14-ff43-48d4-b801-5adc31bbc6fb
EventIdentifier : 1
Sender : System.Timers.Timer
SourceEventArgs : System.Timers.ElapsedEventArgs
SourceArgs : {System.Timers.Timer, System.Timers.ElapsedEventArgs}
SourceIdentifier : Timer.Elapsed
TimeGenerated : 4/23/2020 2:30:37 PM
MessageData :
Example 4: Wait for an event after a specified timeout
This example waits up to 90 seconds for the next event that is raised and that has a source identifier of ProcessStarted. If the specified time expires, the wait ends.
Wait-Event -SourceIdentifier "ProcessStarted" -Timeout 90
Parámetros
-SourceIdentifier
Specifies the source identifier that this cmdlet waits for events.
By default, Wait-Event
waits for any event.
Propiedades del parámetro
Tipo: | String |
Valor predeterminado: | None |
Admite caracteres comodín: | False |
DontShow: | False |
Conjuntos de parámetros
(All)
Posición: | 0 |
Mandatory: | False |
Valor de la canalización: | False |
Valor de la canalización por nombre de propiedad: | True |
Valor de los argumentos restantes: | False |
-Timeout
Specifies the maximum time, in seconds, that Wait-Event
waits for the event to occur. The default,
-1, waits indefinitely. The timing starts when you submit the Wait-Event
command.
If the specified time is exceeded, the wait ends and the command prompt returns, even if the event has not been raised. No error message is displayed.
Propiedades del parámetro
Tipo: | Int32 |
Valor predeterminado: | -1 |
Admite caracteres comodín: | False |
DontShow: | False |
Alias: | TimeoutSec |
Conjuntos de parámetros
(All)
Posición: | Named |
Mandatory: | False |
Valor de la canalización: | False |
Valor de la canalización por nombre de propiedad: | False |
Valor de los argumentos restantes: | False |
CommonParameters
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.
Entradas
String
Salidas
PSEventArgs
Notas
Events, event subscriptions, and the event queue exist only in the current session. If you close the current session, the event queue is discarded and the event subscription is canceled.