Wait-Event
Waits until a particular event is raised before continuing to run.
语法
Default (默认值)
Wait-Event
[[-SourceIdentifier] <String>]
[-Timeout <Int32>]
[<CommonParameters>]
说明
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: by using the Action parameter of the event subscription and by waiting for an event to return and then respond with an action.
示例
Example 1: Wait for the next event
PS C:\> Wait-Event
This command waits for the next event that is raised.
Example 2: Wait for an event with a specified source identifier
PS C:\> Wait-Event -SourceIdentifier "ProcessStarted"
This command waits for the next event that is raised and that has a source identifier of ProcessStarted.
Example 3: Wait for a timer elapsed event
PS C:\> $Timer.Interval = 2000
PS C:\> $Timer.Autoreset = $False
PS C:\> $Timer.Enabled = $True; Wait-Event Timer.Elapsed
# After 2 seconds
EventIdentifier : 12
Sender : System.Timers.Timer
SourceEventArgs : System.Timers.ElapsedEventArgs
SourceArgs : {System.Timers.Timer, System.Timers.ElapsedEventArgs}
SourceIdentifier : Timer.Elapsed
TimeGenerated : 6/10/2008 3:24:18 PM
MessageData :
ForwardEvent : False
This command uses the Wait-Event cmdlet to wait for a timer event on a timer that is set for 2000 milliseconds.
Example 4: Wait for an event after a specified timeout
PS C:\> Wait-Event -SourceIdentifier "ProcessStarted" -Timeout 90
This command 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.
参数
-SourceIdentifier
Specifies the source identifier that this cmdlet waits for events. By default, Wait-Event waits for any event.
参数属性
类型: | String |
默认值: | None |
支持通配符: | False |
不显示: | False |
参数集
(All)
Position: | 0 |
必需: | False |
来自管道的值: | False |
来自管道的值(按属性名称): | True |
来自剩余参数的值: | 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.
参数属性
类型: | Int32 |
默认值: | None |
支持通配符: | False |
不显示: | False |
别名: | TimeoutSec |
参数集
(All)
Position: | Named |
必需: | False |
来自管道的值: | False |
来自管道的值(按属性名称): | False |
来自剩余参数的值: | 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.
输入
String
输出
PSEventArgs
备注
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.