basic example for a timer with ShowUI

Feb 3, 2014 at 9:39 PM
Edited Feb 10, 2014 at 9:08 AM
I read the
"Updating Controls From Timers Example"
and downsized the code to a very basic example.
In some of my tests the timer lives longer then the window,
so I put also code to stop the timer in the window OnClosing Event.

This is my first thread to CodePlex.
My code was written with PowerShell-ise
Please tell me how to copy code with colored script.

After advice I clolored the script with ```posh

Erhy
Import-Module ShowUi

New-Window -Title 'Timertest' -SizeToContent "WidthAndHeight" -Name MyWindow {
    New-Label -Name lbl_UnBound -Content 'Original Unbound Content'
} -On_Loaded {
############## T I M E R declarations in the window OnLoaded Event ###############
    <#mandatory#>$scripts = Get-Resource -Visual $window -Name Scripts #ShowUi Script Collection
    # Event Handler executed on each Tick of the timer
    # ---adapt handler as needed!---
    $scripts.EvtHandlerForTimer = { 
        write-host ("EvtHandlerForTimer " + $MyTimerObject  + " starting.")
        $iterations++
        $lbl_UnBound.Content = $iterations = $iterations % 10 
    } # End of adapt as needed

    <#mandatory#>$MyTimerObject = (New-Object Windows.Threading.DispatcherTimer).PSObject.BaseObject
    # ---adapt interval as needed!----
    $MyTimerObject.Interval = "0:0:2" # every 2 seconds
    <#mandatory#>$MyTimerObject.add_Tick($window.Resources.Scripts.EvtHandlerForTimer); <#mandatory#>$MyTimerObject.Start()
############## end of T I M E R declarations ###############

} -On_Closing {
############## unload T I M E R in the window OnClosing Event ###############
    <#mandatory#>$MyTimerObject.Stop() <#stop timer otherwise it lifes also without the window#>; <#mandatory#>$MyTimerObject = $null #free the timer object to be unloaded
############## end of unload T I M E R ###############
} -show

Coordinator
Feb 3, 2014 at 11:19 PM
The logical way to do that is with the Register-PowerShellCommand.

When you create a variable like $MyTimerObject inside your event handler, the variable isn't available to the on_closing event unless you make it GLOBAL scope, so that will fail to stop or free it.

Also, CodePlex uses a modified Markdown for these forums, click the ? icon on the toolbar to see help. You need to wrap your code with three backticks ``` on a line by themselves (you can add the word "posh" after the first three to specify the highlighting should be PowerShell, I always do)
New-Label -name Time -FontSize 24 -On_Loaded {
    Register-PowerShellCommand -ScriptBlock {     
        # Note: $window works here because it's magic, 
        # but $Time doesn't, because this isn't an event handler for the control, it's for the PowerShellCommand timer.
        $window.Content.Content = (Get-Date | Out-String).Trim()
    } -run -interval "0:0:0.05"
} -asjob
You could fix the variables by manually calling the Initialize-EventHandler (it should be automatic, but we screwed it up). This way, you should be able to refer to your control by name, regardless of how deep in the window it is:
New-Label -name Time -FontSize 24 -On_Loaded {
    Register-PowerShellCommand -ScriptBlock {     
        # Note: $window works here because it's magic, so initialize variables for other controls by name:
        . Initialize-EventHandler -parent $Window
        $Time.Content = (Get-Date | Out-String).Trim()
    } -run -interval "0:0:0.05"
} -asjob
Marked as answer by Erhy on 2/12/2014 at 5:17 AM
Feb 4, 2014 at 11:29 AM
At first I thougt also that the variable $MyTimerObject
can not be available to the on_closing event unless, if it is declarated in on-load event.
But if I write
-On_Closing {
    write-host ( "in Window OnClosing Event `$MyTimerObject.gettype()`: " + $MyTimerObject.gettype() )
   # I see the message: in Window OnClosing Event $MyTimerObject.gettype(): System.Windows.Threading.DispatcherTimer 
}
Your example with Register-PowerShellCommand is really magic for me,
especially while I suppose that the -interval option creates a timer

Erhy