Timer in packaged script

Ask your Windows 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.
Locked
User avatar
daniel kusnir
Posts: 40
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Thu May 25, 2017 8:22 am

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.  
  3. $timer.Interval = 1000 #1 second
  4.  
  5. $complete = 0
  6.  
  7.  
  8.  
  9. $action = {
  10.  
  11.  
  12.  
  13.     if(! ($complete -eq 1) )
  14.  
  15.     {
  16.  
  17.         write-output "Timer still running"
  18.  
  19.     }
  20.  
  21.    
  22.  
  23.     else
  24.  
  25.     {
  26.  
  27.         write-output "Timer ended"
  28.  
  29.         $timer.stop()
  30.  
  31.         Unregister-Event thetimer -Force
  32.  
  33.     }
  34.  
  35. }
  36.  
  37.  
  38.  
  39. Register-ObjectEvent -InputObject $timer -EventName elapsed -SourceIdentifier thetimer -Action $action
  40.  
  41.  
  42.  
  43. $timer.start()

jvierra
Posts: 14368
Joined: Tue May 22, 2007 9:57 am
Contact:

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: 40
Joined: Thu May 25, 2017 8:22 am

Re: Timer in packaged script

Post by daniel kusnir »

:) as always, that did the trick

thanks a lot

Locked