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.
jvierra
Posts: 14543
Joined: Tue May 22, 2007 9:57 am
Contact:

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: 14543
Joined: Tue May 22, 2007 9:57 am
Contact:

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: 27
Joined: Mon Dec 30, 2019 10:58 am

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 24 times

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

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: 27
Joined: Mon Dec 30, 2019 10:58 am

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: 14543
Joined: Tue May 22, 2007 9:57 am
Contact:

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: 27
Joined: Mon Dec 30, 2019 10:58 am

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: 14543
Joined: Tue May 22, 2007 9:57 am
Contact:

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: 27
Joined: Mon Dec 30, 2019 10:58 am

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: 14543
Joined: Tue May 22, 2007 9:57 am
Contact:

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.

Locked