title: System.Windows.Forms.Timer Tick interval shorter than defined interval
body: <p>I am developing a Powershell script using System.Windows.Forms to monitor and process contents of a folder. To automatically check the contents of a folder i used System.Windows.Forms.Timer with an interval of 2000ms. Each Tick the function Timer_Tick is invoked, which in its turn invokes another function.</p>
  <p>When starting the script the functions are immediately invoked dozens of times in a second and then slows down. I added some <code>Write-Host</code> statements clarify the issue. After starting the script
  the WaitForStart and Timer_Tick functions are invoked multiple times per second. My expectation was that the functions would have been invoked (approx.) once every 2 seconds.</p>
  <pre><code>Printen van dossier naar hotfo
  Form load
  Timer Tick + 17:03:369637
  Wait for Start  + 17:03:369637
  Timer Tick + 17:03:369735
  Wait for Start  + 17:03:369735
  Timer Tick + 17:03:369794
  Wait for Start  + 17:03:369794
  Timer Tick + 17:03:369882
  Wait for Start  + 17:03:369882
  Timer Tick + 17:03:369970
  Wait for Start  + 17:03:369970
  Timer Tick + 17:03:370009
  Wait for Start  + 17:03:370009
  Timer Tick + 17:03:370057
  Wait for Start  + 17:03:370057
  Timer Tick + 17:03:370145
  Wait for Start  + 17:03:370145
  Timer Tick + 17:03:370204
  Wait for Start  + 17:03:370204
  Timer Tick + 17:03:370282
  Wait for Start  + 17:03:370282
  Timer Tick + 17:03:370360
  Wait for Start  + 17:03:370360
  Timer Tick + 17:03:370448
  Wait for Start  + 17:03:370448
  Timer Tick + 17:03:370546
  Wait for Start  + 17:03:370546
  Timer Tick + 17:03:370614
  Wait for Start  + 17:03:370614
  Timer Tick + 17:03:370673
  Wait for Start  + 17:03:370673
  Timer Tick + 17:03:370741
  Wait for Start  + 17:03:370741
  Timer Tick + 17:03:370829
  Wait for Start  + 17:03:370829
  Timer Tick + 17:03:370907
  </code></pre>
  <p>The powershell script</p>
  <pre><code>Param (
          # Define parameters below, each separated by a comma
  
          [Parameter(Mandatory)]
          [string]$Dossierfolder,
  
          [Parameter(Mandatory)]
          [string]$Hotfolder
  )
  
  Add-Type -AssemblyName System.Windows.Forms
  
  $nl = &quot;`r`n&quot;
  
  $font_small = 'Microsoft Sans Serif,12'
  $font_large = 'Microsoft Sans Serif,18'
  
  $LocalPrinterForm                   = New-Object system.Windows.Forms.Form
  $LocalPrinterForm.ClientSize        = '500,300'
  $LocalPrinterForm.text              = &quot;LazyAdmin - PowerShell GUI Example&quot;
  $LocalPrinterForm.BackColor         = &quot;#ffffff&quot;
  
  
  $Heading                            = New-Object system.Windows.Forms.Label
  $Heading.text                       = &quot;OVD Hotfolder printer&quot;
  $Heading.AutoSize                   = $true
  $Heading.width                      = 25
  $Heading.height                     = 10
  $Heading.location                   = New-Object System.Drawing.Point(20,20)
  $Heading.Font                       = $font_large
  
  $DossierFolderLabel                 = New-Object system.Windows.Forms.Label
  $DossierFolderLabel.text            = &quot;Dossier folder :&quot;
  $DossierFolderLabel.AutoSize        = $true
  $DossierFolderLabel.width           = 25
  $DossierFolderLabel.height          = 10
  $DossierFolderLabel.location        = New-Object System.Drawing.Point(20,50)
  $DossierFolderLabel.Font            = $font_small
  
  $HotFolderLabel                     = New-Object system.Windows.Forms.Label
  $HotFolderLabel.text                = &quot;Hot folder :&quot;
  $HotFolderLabel.AutoSize            = $true
  $HotFolderLabel.width               = 25
  $HotFolderLabel.height              = 10
  $HotFolderLabel.location            = New-Object System.Drawing.Point(20,70)
  $HotFolderLabel.Font                = $font_small
  
  $StatusLabel                        = New-Object system.Windows.Forms.Label
  $StatusLabel.text                   = &quot;STATUS :&quot;
  $StatusLabel.AutoSize               = $true
  $StatusLabel.width                  = 25
  $StatusLabel.height                 = 10
  $StatusLabel.location               = New-Object System.Drawing.Point(20,100)
  $StatusLabel.Font                   = $font_small
  
  $ProgressIndicator                  = New-Object system.Windows.Forms.ProgressBar
  $ProgressIndicator.Minimum          = 1
  $ProgressIndicator.Maximum          = 25
  $ProgressIndicator.Value            = 12
  $ProgressIndicator.Step             = 1
  $ProgressIndicator.width            = 450
  $ProgressIndicator.height           = 20
  $ProgressIndicator.location         = New-Object System.Drawing.Point(20,140)
  
  $Logging                            = New-Object system.Windows.Forms.TextBox
  $Logging.MultiLine                  = $true
  $Logging.width                      = 450
  $Logging.height                     = 100
  $Logging.ScrollBars                 = &quot;vertical&quot;;
  $Logging.location                   = New-Object System.Drawing.Point(20,160)
  
  $LocalPrinterForm.controls.AddRange(@($Heading,$DossierFolderLabel,$HotFolderLabel,$StatusLabel,$ProgressIndicator, $Logging))
  
  Write-Host &quot;Printen van dossier naar hotfolder&quot;
  
  &lt;# Start with only when hotfolder is empty #&gt;
  $hotfolderfiles = Get-ChildItem $Hotfolder -Filter *.pdf 
  $hotfoldernumfiles = $hotfolderfiles.Count 
  
  if ($hotfoldernumfiles -ne 0) {
      [System.Windows.MessageBox]::Show('Hotfolder is niet, leeg hotfolder &quot;' + 
          $Hotfolder + '&quot; en start hotfolderprint opnieuw','Waarschuwing','Ok')
      exit
  }
  
  &lt;# Set timer #&gt;
  $timer = New-Object System.Windows.Forms.Timer
  $timer.Interval = 2000
  
  &lt;# Processing each interval #&gt;
  Function WaitForStart {
      
      &lt;# $HotFolderLabel.Text = Get-Date -Format &quot;HH:mm:ss&quot;#&gt;
      Write-Host &quot;Wait for Start &quot; + $DossierFolderLabel.Text
      $Logging.AppendText($HotFolderLabel.Text + $nl )
  }
  
  &lt;# Start Timer on Load #&gt;
  Function Form_Load {
      $timer.Start()
      Write-Host &quot;Form load&quot;
  }
  
  &lt;# Interval Handling #&gt;
  Function Timer_Tick {
      $DossierFolderLabel.Text = Get-Date -Format &quot;HH:mm:ssffff&quot;
      Write-Host &quot;Timer Tick&quot; + $DossierFolderLabel.Text
      WaitForStart
  }
  
  $LocalPrinterForm.Add_Load({Form_Load})
  $timer.add_tick({Timer_Tick})
  
  [void]$LocalPrinterForm.ShowDialog()
  </code></pre>