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
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 »

DateTimes should only be checked against a datetime object or you can have issues.

Run you code in teh debugger to see what is being executed and where it is hanging. The form will freeze if you are stuck in a loop.
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 seem to be sitting in the timer tick. You cannot do this. Let the timer do the changes based on the time and not on linear code in the timer event.

Unfortunately thee is no way for me to guess at what you are trying to do and your code is not helpful.

Post the simplest PSF file you can that illustrates your issue.
mattys
Posts: 62
Last visit: Wed Dec 27, 2023 8:28 am
Has voted: 3 times

Re: Timer Event Help

Post by mattys »

Yes, Jvierra. You are right, I'm sitting in the timer tick. :|
In this every 2 mins the label will flash...
digitalClocktest.psf
(23.92 KiB) Downloaded 141 times
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 cannot sit in an event. You must do a task and exit the event. You need to track the states you want to control and use time to tell you wen they need to be executed.
mattys
Posts: 62
Last visit: Wed Dec 27, 2023 8:28 am
Has voted: 3 times

Re: Timer Event Help

Post by mattys »

OK based on what you are saying, I am using time to tell when they need to be executed. I provided a small example digitalClocktest.psf
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 »

My first suggestion is to use an animated GIF to flash the message. This will eliminate one timer tasks. Next use time and not strings and make your decision based on the "Minutes" value of the time object which is an integer. It will be equal to 0 or 30 every 30 minutes. Run the timer at 1 minute ticks and check for those values.

This will simplify the logic and the code.

The hardest thing for those new to programming is to understand how to design a solution. This takes learning and experience. First learn all of teh available controls and how they are used then decide how to use those tools to accomplish a your tasks.

You approach can be made to work but stopping in a timer tick will only freeze the form.
mattys
Posts: 62
Last visit: Wed Dec 27, 2023 8:28 am
Has voted: 3 times

Re: Timer Event Help

Post by mattys »

Thanks for your suggestions, Jvierra. Great ideas. I will make these changes in my project.
Im grateful for your time!!
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 »

Here is a simple example of some simplifications of the code:

Code: Select all

$timer1_Tick={
     # set timer tick to 500ms so it detects by seconds.
    $buttonclock.Text = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId([datetime]::Now, 'US Eastern Standard Time').ToString("h:mm:ss:tt")
    
	if ([datetime]::Now.Minute -in (0,30) -and [datetime]::Now.Second -eq 0){
		
		$timer1.Stop()
		$labeltime.Visible = $true
		$labeltime.Text = "Stay Active!!!"
		$labeltime.BackColor = 'highlight'
		$labeltime.ForeColor = 'yellow'
		Start-Sleep -Milliseconds 255
		$labeltime.BackColor = 'yellow'
		$labeltime.ForeColor = 'highlight'
		Start-Sleep -Milliseconds 255
		$labeltime.BackColor = 'highlight'
		$labeltime.ForeColor = 'yellow'
		Start-Sleep -Milliseconds 255
		$labeltime.Visible = $false
		
		#Resume Clock
		$timer1.Start()
	}
}
mattys
Posts: 62
Last visit: Wed Dec 27, 2023 8:28 am
Has voted: 3 times

Re: Timer Event Help

Post by mattys »

I ended up creating an animated gif to use. I stopped the clock and started a separate short timer for the animated image to play out. Thanks for your help.
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 »

An animated GIF does not require a timer. It blinks by itself. That is why it is called and "animated gif". Search a bit to get more info.
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