Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 882671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:27:47+00:00 2026-05-15T12:27:47+00:00

Consider this code: PS> $timer = New-Object Timers.Timer PS> $timer.Interval = 1000 PS> $i

  • 0

Consider this code:

PS> $timer = New-Object Timers.Timer
PS> $timer.Interval = 1000
PS> $i = 1;
PS> Register-ObjectEvent $timer Elapsed -Action { write-host 'i: ' $i }.GetNewClosure()
PS> $timer.Enabled = 1
i:  1
i:  1
i:  1
 ...
# wait a couple of seconds and change $i
PS> $i = 2
i:  2
i:  2
i:  2

I assumed that when I create new closure ({ write-host 'i: ' $i }.GetNewClosure()) value of $i will be tied to this closure. But not in this case. Afer I change the value, write-host takes the new value.

On the other side, this works:

PS> $i = 1;
PS> $action = { write-host 'i: ' $i }.GetNewClosure()
PS> &$action
i:  1
PS> $i = 2
PS> &$action
i:  1

Why it doesn’t work with the Register-ObjectEvent?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:27:47+00:00Added an answer on May 15, 2026 at 12:27 pm

    Jobs are executed in a dynamic module; modules have isolated sessionstate, and share access to globals. PowerShell closures only work within the same sessionstate / scope chain. Annoying, yes.

    -Oisin

    p.s. I say “jobs” because event handlers are effectively local jobs, no different than script being run with start-job (local machine only, implicit, not using -computer localhost)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 536k
  • Answers 536k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer $newEndDate = strtotime(date("Y-m-d", strtotime($date)) . " +1 month"); May 17, 2026 at 1:18 am
  • Editorial Team
    Editorial Team added an answer I suspect that when you debug your code, you'll see… May 17, 2026 at 1:18 am
  • Editorial Team
    Editorial Team added an answer I think that you can solve your "how do I… May 17, 2026 at 1:18 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Consider this code... using System.Threading; //... Timer someWork = new Timer( delegate(object state) {
Consider this scenario. I have an object, lets call it.... Foo. Foo raises a
Consider the following code parts: this is Timing.h: class Timing { public: //creates a
Consider the following snippet of code: <div align=Left> <font style='font-size:17pt; font-family:Times New Roman;color:#000000;'> <textformat
Consider the following code, I have a textblock that is an on/off button in
Consider the following PHP code: <?php require_once(myDBclass.php); class a { private $tablename; private $column;
< backgound> I'm at a point where I really need to optimize C++ code.
I'm kind of new to Ruby and some of the closure logic has me
so got a new problem... I'm writing a multithreaded proxychecker in c#. I'm using
I have to write a Windows service that handles at some point confidential data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.