等到引发特定事件后再继续运行。

语法

Wait-Event [[-SourceIdentifier] <string>] [-Timeout <int>] [<CommonParameters>]

说明

Wait-Event cmdlet 将挂起脚本或函数的执行,直到引发特定事件。在检测到事件后会继续执行。若要取消等待,请按 CTRL+C。

此功能为事件的轮询提供了一种替代方法。它还允许您通过两种方式确定对事件的响应:使用事件订阅的 Action 参数;等待事件返回,然后通过一个操作进行响应。

参数

-SourceIdentifier <string>

只等待具有指定源标识符的事件。默认情况下,Wait-Event 等待任何事件。

是否为必需?

false

位置?

1

默认值

All events

是否接受管道输入?

true (ByPropertyName)

是否接受通配符?

false

-Timeout <int>

确定 Wait-Event 等待事件发生的最长时间,以秒为单位。默认值为 -1,表示无限期地等待。计时从您提交 Wait-Event 命令时开始。

如果超过指定时间,则等待结束,命令提示符返回,即使尚未引发事件也是如此。不显示错误消息。

是否为必需?

false

位置?

named

默认值

是否接受管道输入?

false

是否接受通配符?

false

<CommonParameters>

此 cmdlet 支持通用参数:-Verbose、-Debug、-ErrorAction、-ErrorVariable、-OutBuffer 和 -OutVariable。有关详细信息,请参阅 about_Commonparameters.

输入和输出

输入类型是指可通过管道传递给 cmdlet 的对象的类型。返回类型是指 Cmdlet 所返回对象的类型。

输入

System.String

输出

System.String

说明

事件、事件订阅和事件队列仅存在于当前会话中。如果关闭当前会话,将丢弃事件队列并取消事件订阅。

示例 1

C:\PS>wait-event

说明
-----------
此命令等待引发下一个事件。






示例 2

C:\PS>wait-event -sourceIdentifier "ProcessStarted"

说明
-----------
此命令等待引发具有源标识符“ProcessStarted”的下一个事件。






示例 3

C:\PS>$timer.Interval = 2000 
C:\PS> $timer.Autoreset = $false 
C:\PS> $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

说明
-----------
此命令使用 Wait-Event cmdlet 等待一个定时器事件,定时器设置为 2000 毫秒。






示例 4

C:\PS>wait-event -sourceIdentifier "ProcessStarted" -timeout 90

说明
-----------
此命令等待引发具有源标识符“ProcessStarted”的下一个事件的最长时间为 90 秒。如果达到该指定时间,则等待结束。






另请参阅




目录