# Script with Register-ObjectEvent hanging

**URL:** https://forums.powershell.org/t/script-with-register-objectevent-hanging/23809
**Category:** PowerShell Help
**Created:** [March 18, 2024, 9:36pm UTC](https://forums.powershell.org/t/script-with-register-objectevent-hanging/23809 "2024-03-18T21:36:05Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![RileyWooten](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@RileyWooten](https://forums.powershell.org/u/RileyWooten)
#### Post date: [March 18, 2024, 9:36pm UTC](https://forums.powershell.org/t/script-with-register-objectevent-hanging/23809/1 "2024-03-18T21:36:05Z")

</div>

Hi!

This is the script in Example 3 of the Wait-Event [page](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/wait-event?view=powershell-7.4#example-3-wait-for-a-timer-elapsed-event), with some little modifications.

```auto
$Timer = New-Object Timers.Timer

$objectEventArgs = @{
    InputObject = $Timer
    EventName = 'Elapsed'
    SourceIdentifier = 'Timer.Elapsed'
    Action = { Write-Host "Action triggered" }
}

$handlers = Register-ObjectEvent @objectEventArgs

$Timer.Interval = 4000
$Timer.Autoreset = $False
$Timer.Enabled = $True

Wait-Event Timer.Elapsed

Unregister-Event -SourceIdentifier 'Timer.Elapsed'
$handlers | Remove-Job

```

If I run it on PowerShell 7.4.1, two problems show up:

1. After printing `Action triggered` it hangs. Shouldn’t the execution continue? How to make it go on?  
If instead there is no `Action` argument at all, the script ends as expected.
2. With no `Action` argument, if I try to run the script two consecutive times in the same PowerShell console, the second time it ends apparently normally, but **without** waiting for the timer to expire: it ends instantaneously. Why?

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [March 19, 2024, 5:04am UTC](https://forums.powershell.org/t/script-with-register-objectevent-hanging/23809/2 "2024-03-19T05:04:17Z")

</div>

I’m not sure on the action triggered . I reproduced that in 7.4 and 5. [PowerShell, Return from “Wait-Event” when “Register-ObjectEvent -Action” triggers? - Stack Overflow](https://stackoverflow.com/questions/7350834/powershell-return-from-wait-event-when-register-objectevent-action-trigger) indirectly suggests that using action with wait-event is a ‘no-no’ but unfortunately they link to another thread that’s no longer available. I don’t know enough about those commands work to give you an intelligent answer than, it doesn’t seem like doing these two things together work. I don’t have time unfortunately to dig/google around more.

on the second thing, I think it’s because you already waited for the event. The timer has elapsed. If you were to change the autoreset to `$true`, and run `Get-Event`, you’ll see every 4 seconds a new event is tirggered in that PS session, but `Wait-Event` I think is simply waiting for you timer to elapse 4 seconds, which it has already.

Are you just playing around, or are you trying to solve an actual issue? If so, could you share the problem you are trying to solve, as there might be another way to address it?

Thank you.

---

<div class="post-metadata">

### Author: ![RileyWooten](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@RileyWooten](https://forums.powershell.org/u/RileyWooten)
#### Post date: [March 19, 2024, 11:21am UTC](https://forums.powershell.org/t/script-with-register-objectevent-hanging/23809/3 "2024-03-19T11:21:24Z")

</div>

> [@dotnVo](#):
>
> I’m not sure on the action triggered . I reproduced that in 7.4 and 5. [PowerShell, Return from “Wait-Event” when “Register-ObjectEvent -Action” triggers? - Stack Overflow](https://stackoverflow.com/questions/7350834/powershell-return-from-wait-event-when-register-objectevent-action-trigger) indirectly suggests that using action with wait-event is a ‘no-no’ but unfortunately they link to another thread that’s no longer available.

No problem, it’s anyway useful, thank you!  
A simple workaround is to put the same code of the `Action` block right after `Wait-Event Timer.Elapsed`:

```auto
Wait-Event Timer.Elapsed
Write-Host "Action triggered"

```

It works this way (even if automatic variables like `$Event`, `$Sender`, etc. will not be available).

> [@dotnVo](#):
>
> on the second thing, I think it’s because you already waited for the event. The timer has elapsed.

Ok, so IIUC the timer expiration (that is: this specific event) can be detected by `Wait-Event` only once in that single PowerShell terminal. Once it has been consumed, `Wait-Event` is done.

> [@dotnVo](#):
>
> Are you just playing around, or are you trying to solve an actual issue? If so, could you share the problem you are trying to solve, as there might be another way to address it?

Both the things. I am playing around and trying to better understand the `Register-ObjectEvent` and `Wait-Event` usages, because of the issues I’m having in [this thread](https://forums.powershell.org/t/configure-from-powershell-a-script-run-when-event-occurs/23677).

Thank you so much!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)
#### Post date: [April 18, 2024, 11:21am UTC](https://forums.powershell.org/t/script-with-register-objectevent-hanging/23809/4 "2024-04-18T11:21:44Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
