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.
This topic is 3 years and 9 months 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
mattys
Posts: 62
Last visit: Wed Dec 27, 2023 8:28 am
Has voted: 3 times

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. $NyTime = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId((Get-Date), 'US Eastern Standard Time')
  3. $buttonclock.Text = $NyTime.ToString("h:mm:ss:tt")
  4. $NexthalfHour = [DateTime]::Today.AddHours([Math]::Round([DateTime]::Now.TimeOfDay.TotalHours * 8) / 8)
  5.  
  6.     if ($NyTime -eq $NexthalfHour)
  7.     {
  8.         $labeltime.Visible = $true
  9.         $labeltime.Text = "Stay Active!!!"
  10.         $labeltime.BackColor = 'highlight'
  11.         $labeltime.ForeColor = 'yellow'
  12.         Start-Sleep 1
  13.         $labeltime.BackColor = 'yellow'
  14.         $labeltime.ForeColor = 'highlight'
  15.  
  16.             #Add TotalTime to current time
  17.         $script:StartTime = (Get-Date).AddSeconds($TotalTime)
  18.         #Start the timer
  19.         $timerUpdate.Start()
  20. }
Then this is the timer for the countdown
  1. $TotalTime = 9 #in seconds
  2. $timerUpdate_Tick = {
  3.     #Use Get-Date for Time Accuracy
  4.     [TimeSpan]$span = $script:StartTime - (Get-Date)
  5.    
  6.     #Update the display
  7.     $formClockAlpha.Text = $labelTime.Text = "{0:N0}" -f $span.TotalSeconds + " Seconds"
  8.    
  9.     if ($span.TotalSeconds -le 0)
  10.     {
  11.         $timerUpdate.Stop()
  12.         #Update the display
  13.         $labeltime.Visible = $false
  14.         $formClockAlpha.Text = "Clock Alpha"
  15.     }
  16. }

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: 1654
Last visit: Thu Mar 28, 2024 6:54 am
Answers: 39
Been upvoted: 30 times

Re: Timer Event Help

Post by brittneyr »

[Topic moved by moderator to PowerShell GUIs forum]
Brittney
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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: 62
Last visit: Wed Dec 27, 2023 8:28 am
Has voted: 3 times

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.     $buttonclock.Enabled = $true
  3.  
  4.     $NyTime = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId((Get-Date), 'US Eastern Standard Time')
  5.    
  6.     $NexthalfHour = $NyTime::Today.AddHours([Math]::Round($NyTime::Now.TimeOfDay.TotalHours * 2) / 2)
  7.    
  8.    
  9.     if ($NyTime -eq $NexthalfHour)
  10.     {  
  11.         $buttonclock.Visible = $false
  12.         $labeltime.Visible = $true
  13.         $labeltime.Text = "Stay Active!!!"
  14.         $labeltime.BackColor = 'highlight'
  15.         $labeltime.ForeColor = 'yellow'
  16.        
  17.         Start-Sleep 1
  18.         $labeltime.BackColor = 'yellow'
  19.         $labeltime.ForeColor = 'highlight'
  20.         ####
  21.         Start-Sleep 1
  22.         $labeltime.BackColor = 'highlight'
  23.         $labeltime.ForeColor = 'yellow'
  24.        
  25.         Start-Sleep 1
  26.         $labeltime.BackColor = 'yellow'
  27.         $labeltime.ForeColor = 'highlight'
  28.         Start-Sleep 1
  29.         $labeltime.BackColor = 'highlight'
  30.         $labeltime.ForeColor = 'yellow'
  31.        
  32.         Start-Sleep 1
  33.        
  34.         $buttonclock.Visible = $true
  35.         $labeltime.Visible = $false
  36.        
  37.     }
  38.     else
  39.     {
  40.         $NyTime = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId((Get-Date), 'US Eastern Standard Time')
  41.        
  42.         $buttonclock.Text = $NyTime.ToString("h:mm:ss:tt")
  43.        
  44.     }
  45.  
  46. }
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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: 62
Last visit: Wed Dec 27, 2023 8:28 am
Has voted: 3 times

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Timer Event Help

Post by jvierra »

On the tick check the time. It is the right time then execute the code.
mattys
Posts: 62
Last visit: Wed Dec 27, 2023 8:28 am
Has voted: 3 times

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Timer Event Help

Post by jvierra »

You are welcome. Did you find a way to do this?
mattys
Posts: 62
Last visit: Wed Dec 27, 2023 8:28 am
Has voted: 3 times

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
This topic is 3 years and 9 months 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