Timer in packaged script

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 4 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
daniel kusnir
Posts: 75
Last visit: Tue May 23, 2023 10:50 am
Answers: 1

Timer in packaged script

Post by daniel kusnir »

hello

i have following code that does not seemt to run when i repackage script to the exe. Is there something else in terms of timer and repackaged script ?
i read the register-objectevent should not be used directly in WinForms, but did not read about simple script. When run in the console as a code, no issues are there. Its not a problem of the output, but it seems that code inside action is not executed
  1. $timer = new-object system.timers.timer
  2. $timer.Interval = 1000 #1 second
  3. $complete = 0
  4.  
  5. $action = {
  6.  
  7.     if(! ($complete -eq 1) )
  8.     {
  9.         write-output "Timer still running"
  10.     }
  11.    
  12.     else
  13.     {
  14.         write-output "Timer ended"
  15.         $timer.stop()
  16.         Unregister-Event thetimer -Force
  17.     }
  18. }
  19.  
  20. Register-ObjectEvent -InputObject $timer -EventName elapsed -SourceIdentifier thetimer -Action $action
  21.  
  22. $timer.start()
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Timer in packaged script

Post by jvierra »

The code will exit before the timer ticks since the code ends after the timer start.

Add this to the end:

while(1){sleep 1}
User avatar
daniel kusnir
Posts: 75
Last visit: Tue May 23, 2023 10:50 am
Answers: 1

Re: Timer in packaged script

Post by daniel kusnir »

:) as always, that did the trick

thanks a lot
This topic is 4 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked