在 PowerShell 中订阅对象的静态事件的语法是什么?

发布于 2024-08-22 18:49:19 字数 906 浏览 5 评论 0原文

Register-ObjectEvent 在所需参数中查找对象实例输入对象。对象的静态(共享)事件的语法是什么?

更新TimeChanged<的正确语法/a>:

$systemEvents = [Microsoft.Win32.SystemEvents]
$timeChanged = Register-ObjectEvent -InputObject $systemEvents
-EventName 'TimeChanged' -Action { Write-Host "Time changed" }

不幸的是,PowerShell ISE 中不会发出系统事件信号。下面是一个使用对象静态事件的示例,该事件在任何地方都适用:

$networkInformation = [System.Net.NetworkInformation.NetworkChange];
$networkAddressChanged = Register-ObjectEvent -InputObject $networkInformation 
-EventName 'NetworkAddressChanged' 
-Action { Write-Host "NetworkAddressChanged event signaled" }

Register-ObjectEvent looks for a object instance in the required parameter InputObject. What is the syntax for an object's static (Shared) event?

UPDATE: Correct syntax for TimeChanged:

$systemEvents = [Microsoft.Win32.SystemEvents]
$timeChanged = Register-ObjectEvent -InputObject $systemEvents
-EventName 'TimeChanged' -Action { Write-Host "Time changed" }

Unfortunately, the SystemEvents will not be signaled in PowerShell ISE. Here's a sample using an object's staic event that works everywhere:

$networkInformation = [System.Net.NetworkInformation.NetworkChange];
$networkAddressChanged = Register-ObjectEvent -InputObject $networkInformation 
-EventName 'NetworkAddressChanged' 
-Action { Write-Host "NetworkAddressChanged event signaled" }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蝶…霜飞 2024-08-29 18:49:19

如果将静态类型分配给变量,则可以订阅静态事件。

例如:

$MyStaticType = [MyStaticNamespace.MyStaticClass]
Register-ObjectEvent -InputObject $MyStaticType -EventName MyStaticEvent -Action {Write-Host "Caught a static event"}

要查找类型可能具有的任何静态事件,您可以使用 Get-Member 和 -Static 开关

[MyStaticNamespace.MyStaticClass] | get-member -static -membertype event

编辑:
我确实注意到,当尝试访问 [Microsoft.Win32.SystemEvents] 事件时,我需要在提升的提示符下运行(在 Vista 及更高版本上)才能访问消息。

If you assign a static type to a variable, you can subscribe to static events.

For example:

$MyStaticType = [MyStaticNamespace.MyStaticClass]
Register-ObjectEvent -InputObject $MyStaticType -EventName MyStaticEvent -Action {Write-Host "Caught a static event"}

To find any static events a type may have, you can use Get-Member with the -Static switch

[MyStaticNamespace.MyStaticClass] | get-member -static -membertype event

EDIT:
I did notice when trying to access [Microsoft.Win32.SystemEvents] events, that I needed to be running in an elevated prompt (on Vista and above) in order to access the messages.

剪不断理还乱 2024-08-29 18:49:19

史蒂文得到了正确的答案,因此无需对此进行投票(而是对他的答案进行投票)。我只是想发布一个示例片段,人们可以使用它来处理静态事件,这样您就不必找到易于触发的 BCL 静态事件。 :-)

$src = @'
using System;

namespace Utils {
public static class StaticEventTest 
{
    public static event EventHandler Fired;

    public static void RaiseFired()
    {
        if (Fired != null) 
        { 
            Fired(typeof(StaticEventTest), EventArgs.Empty); 
        }
    }
}}
'@

$srcId = 'Fired'

Add-Type -TypeDefinition $src

Unregister-Event -SourceIdentifier $srcId -ea 0

$id = Register-ObjectEvent ([Utils.StaticEventTest]) Fired `
          -SourceIdentifier $srcId -Action {"The static event fired"}

[Utils.StaticEventTest]::RaiseFired()

while (!$id.HasMoreData) { Start-Sleep -Milliseconds 250 }

Receive-Job $id

Steven's got the right answer so no need to vote on this (vote on his instead). I just wanted to post a sample snippet that folks can use to play around with static events such that you don't have to find a BCL static event that's easy to fire. :-)

$src = @'
using System;

namespace Utils {
public static class StaticEventTest 
{
    public static event EventHandler Fired;

    public static void RaiseFired()
    {
        if (Fired != null) 
        { 
            Fired(typeof(StaticEventTest), EventArgs.Empty); 
        }
    }
}}
'@

$srcId = 'Fired'

Add-Type -TypeDefinition $src

Unregister-Event -SourceIdentifier $srcId -ea 0

$id = Register-ObjectEvent ([Utils.StaticEventTest]) Fired `
          -SourceIdentifier $srcId -Action {"The static event fired"}

[Utils.StaticEventTest]::RaiseFired()

while (!$id.HasMoreData) { Start-Sleep -Milliseconds 250 }

Receive-Job $id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文