Here is a Timer Jobs example

Jun 30, 2011 at 2:23 AM

If you are interested, please feel free to use the following example in a future drop. I wanted to use Register-PowershellJob and Start-PowershellJob, but they don't (yet) support a -Tag parameter to populate the timer's Tag. Maybe in a future release? 

 

New-Window -Title 'Timer Jobs' -SizeToContent "WidthAndHeight" {
  Dockpanel -Name dp {
    Button "1" -Name btn_1 -Tag 1 -Dock Top
    Button "2" -Name btn_2 -Tag 2 -Dock Bottom
  }
} -On_Loaded {
  write-host ("window On_Loaded Event handler starting. window: {0}" -f $window.Title)
  # Create scriptblocks that can be shared amongst event handlers. Good place for handlers for child controls in ItemTemplates
  #  Access the Scripts hashtable in the top-level window's Resources dictionary
  $scripts = Get-Resource -Visual $window -Name Scripts
  # BtnOnClick is the On-Click handler (scriptblock) for the  buttons
  $scripts.BtnOnClick = {
    write-host ("BtnOnClick Handler starting. {0} tagged with {1}" -f $this.Name, $this.Tag)
    # Turn off the button to prevent multiple presses while the Job is running
    $this.IsEnabled = $false
    $mybutton = $this.Tag
    # Create a Timer and associate the TimerOnTick scriptblock with the timer. 
# Wish this could be Register-PowershellJob -Run -In '0:0:1' -Tag $mybutton -Scriptblock $window.Resources.Scripts.TimerOnTick
$window.Resources.Timers."Run-$mybutton" = (New-Object Windows.Threading.DispatcherTimer).PSObject.BaseObject $window.Resources.Timers."Run-$mybutton".Interval = "0:0:1" $window.Resources.Timers."Run-$mybutton".add_Tick($window.Resources.Scripts.TimerOnTick) # By assigning something to the timer's tag, you can pass an argument to the scriptblock $window.Resources.Timers."Run-$mybutton".Tag = $mybutton $window.Resources.Timers."Run-$mybutton".Start() } # TimerOnTick is the On-Tick handler for the timers $scripts.TimerOnTick = { write-host ("TimerOnTick Handler [{0}] starting. Tagged with {1}" -f $this, $this.Tag) } Add-EventHandler $btn_1 Click (Get-Resource -Visual $window -Name "scripts").BtnOnClick Add-EventHandler $btn_2 Click (Get-Resource -Visual $window -Name "scripts").BtnOnClick }-show