Timer Event Help

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
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.
mattys
Posts: 27
Joined: Mon Dec 30, 2019 10:58 am

Timer Event Help

Post by mattys »

I'm attempting to create an app which displays a digital time clock. - I'm using a timer named timer1. No problem.. At 30 min and top of the hour I want the clock to stop/pause or even turn off..and Display a label.

This is an animated gif on my project: https://ibb.co/MPxxxhg

Ive been struggling with this for a while now and am turning to to the board for some guidance.

I have this code in my timer1_Tick event..
  1.    
  2.  
  3. $NyTime = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId((Get-Date), 'US Eastern Standard Time')
  4.  
  5. $buttonclock.Text = $NyTime.ToString("h:mm:ss:tt")
  6.  
  7. $NexthalfHour = [DateTime]::Today.AddHours([Math]::Round([DateTime]::Now.TimeOfDay.TotalHours * 8) / 8)
  8.  
  9.  
  10.  
  11.     if ($NyTime -eq $NexthalfHour)
  12.  
  13.     {
  14.  
  15.         $labeltime.Visible = $true
  16.  
  17.         $labeltime.Text = "Stay Active!!!"
  18.  
  19.         $labeltime.BackColor = 'highlight'
  20.  
  21.         $labeltime.ForeColor = 'yellow'
  22.  
  23.         Start-Sleep 1
  24.  
  25.         $labeltime.BackColor = 'yellow'
  26.  
  27.         $labeltime.ForeColor = 'highlight'
  28.  
  29.  
  30.  
  31.             #Add TotalTime to current time
  32.  
  33.         $script:StartTime = (Get-Date).AddSeconds($TotalTime)
  34.  
  35.         #Start the timer
  36.  
  37.         $timerUpdate.Start()
  38.  
  39. }
Then this is the timer for the countdown
  1. $TotalTime = 9 #in seconds
  2.  
  3. $timerUpdate_Tick = {
  4.  
  5.     #Use Get-Date for Time Accuracy
  6.  
  7.     [TimeSpan]$span = $script:StartTime - (Get-Date)
  8.  
  9.    
  10.  
  11.     #Update the display
  12.  
  13.     $formClockAlpha.Text = $labelTime.Text = "{0:N0}" -f $span.TotalSeconds + " Seconds"
  14.  
  15.    
  16.  
  17.     if ($span.TotalSeconds -le 0)
  18.  
  19.     {
  20.  
  21.         $timerUpdate.Stop()
  22.  
  23.         #Update the display
  24.  
  25.         $labeltime.Visible = $false
  26.  
  27.         $formClockAlpha.Text = "Clock Alpha"
  28.  
  29.     }
  30.  
  31. }

Im able to accomplish the label appearing with a click event, but how do I auto-trigger the event when the clock strikes my desired times?

Im out of bullets on how to accomplish this task.

Thanks in advance.

User avatar
brittneyr
Site Admin
Posts: 538
Joined: Thu Jun 01, 2017 7:20 am

Re: Timer Event Help

Post by brittneyr »

[Topic moved by moderator to PowerShell GUIs forum]
Brittney Ryn
SAPIEN Technologies, Inc.

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

Re: Timer Event Help

Post by jvierra »

The code that accomplishes this should be in the timer event and not in a button. Just place the button code in the timer event.

mattys
Posts: 27
Joined: Mon Dec 30, 2019 10:58 am

Re: Timer Event Help

Post by mattys »

That is why im confused how it is not getting triggered. I do have the button in the timer1_tick event.
  1. $timer1_Tick = {
  2.  
  3.     $buttonclock.Enabled = $true
  4.  
  5.  
  6.  
  7.     $NyTime = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId((Get-Date), 'US Eastern Standard Time')
  8.  
  9.    
  10.  
  11.     $NexthalfHour = $NyTime::Today.AddHours([Math]::Round($NyTime::Now.TimeOfDay.TotalHours * 2) / 2)
  12.  
  13.    
  14.  
  15.    
  16.  
  17.     if ($NyTime -eq $NexthalfHour)
  18.  
  19.     {  
  20.  
  21.         $buttonclock.Visible = $false
  22.  
  23.         $labeltime.Visible = $true
  24.  
  25.         $labeltime.Text = "Stay Active!!!"
  26.  
  27.         $labeltime.BackColor = 'highlight'
  28.  
  29.         $labeltime.ForeColor = 'yellow'
  30.  
  31.        
  32.  
  33.         Start-Sleep 1
  34.  
  35.         $labeltime.BackColor = 'yellow'
  36.  
  37.         $labeltime.ForeColor = 'highlight'
  38.  
  39.         ####
  40.  
  41.         Start-Sleep 1
  42.  
  43.         $labeltime.BackColor = 'highlight'
  44.  
  45.         $labeltime.ForeColor = 'yellow'
  46.  
  47.        
  48.  
  49.         Start-Sleep 1
  50.  
  51.         $labeltime.BackColor = 'yellow'
  52.  
  53.         $labeltime.ForeColor = 'highlight'
  54.  
  55.         Start-Sleep 1
  56.  
  57.         $labeltime.BackColor = 'highlight'
  58.  
  59.         $labeltime.ForeColor = 'yellow'
  60.  
  61.        
  62.  
  63.         Start-Sleep 1
  64.  
  65.        
  66.  
  67.         $buttonclock.Visible = $true
  68.  
  69.         $labeltime.Visible = $false
  70.  
  71.        
  72.  
  73.     }
  74.  
  75.     else
  76.  
  77.     {
  78.  
  79.         $NyTime = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId((Get-Date), 'US Eastern Standard Time')
  80.  
  81.        
  82.  
  83.         $buttonclock.Text = $NyTime.ToString("h:mm:ss:tt")
  84.  
  85.        
  86.  
  87.     }
  88.  
  89.  
  90.  
  91. }

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

Re: Timer Event Help

Post by jvierra »

Not the button. The code that the button executes goes in the timer tick.

To detect the interval you want to not use the ticks but check the system clock modulus 30. This will cause a detection at each 30 minute interval. The code has to set a flag when the interval is detected then clear the flag after the interval is complete. This prevents multiple triggers if teh tick occurs more that once when "minutes" is equal to 0 or 30. THe issue is to always detect teh boundary conditions that occur but time is an issue. Time does not have an absolute boundary. Clock time runs in segments depending on resolution.

Another way to do this is to use a countdown time that events in 30 minutes. Execute your code then rearm the timer and it will only tick again in 30 minutes. The problem with this approach is the countdown timers insert skew. They can be used but require occasional correction. Using the system clock is easier and more reliable.

mattys
Posts: 27
Joined: Mon Dec 30, 2019 10:58 am

Re: Timer Event Help

Post by mattys »

I apologize, Im having a tough time with this.
I'm looking for the trigger to happen at specific times. 7, 7:30, 8, 8:30..
Will the clock modulus work like this?

Also, the code of the button is in the timer.. it just shows the time

Thank you

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

Re: Timer Event Help

Post by jvierra »

On the tick check the time. It is the right time then execute the code.

mattys
Posts: 27
Joined: Mon Dec 30, 2019 10:58 am

Re: Timer Event Help

Post by mattys »

Thank you for your help again, Jvierra. You are a great asset to the board. Thank you!!

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

Re: Timer Event Help

Post by jvierra »

You are welcome. Did you find a way to do this?

mattys
Posts: 27
Joined: Mon Dec 30, 2019 10:58 am

Re: Timer Event Help

Post by mattys »

When I was checking with the IF statement I think I was checking a datetime against a string. So I made them both strings and now it sees the trigger.

My only hiccup now is sometimes the label flash portion will freeze/stall?...Which I don't understand fully because I stop the timer and the labels flashing is a whole 5 seconds. Im not sure how to make it more efficient and reliable?..

Thanks

Locked