系统.Windows.表格.计时器刻度间隔短于定义的间隔



我正在使用System开发Powershell脚本。Windows。用于监视和处理文件夹内容的窗体。为了自动检查文件夹的内容,我使用了System。Windows。表格。计时器,间隔2000ms。每次Tick都会调用函数Timer_Tick,然后调用另一个函数。

启动脚本时,函数会立即在一秒钟内被调用数十次,然后速度会减慢。我添加了一些Write-Host声明澄清了这个问题。启动脚本后WaitForStart和Timer_ Tick函数每秒被调用多次。我的预期是,这些函数大约每2秒调用一次。

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

powershell脚本

Param (
# Define parameters below, each separated by a comma
[Parameter(Mandatory)]
[string]$Dossierfolder,
[Parameter(Mandatory)]
[string]$Hotfolder
)
Add-Type -AssemblyName System.Windows.Forms
$nl = "`r`n"
$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              = "LazyAdmin - PowerShell GUI Example"
$LocalPrinterForm.BackColor         = "#ffffff"

$Heading                            = New-Object system.Windows.Forms.Label
$Heading.text                       = "OVD Hotfolder printer"
$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            = "Dossier folder :"
$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                = "Hot folder :"
$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                   = "STATUS :"
$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                 = "vertical";
$Logging.location                   = New-Object System.Drawing.Point(20,160)
$LocalPrinterForm.controls.AddRange(@($Heading,$DossierFolderLabel,$HotFolderLabel,$StatusLabel,$ProgressIndicator, $Logging))
Write-Host "Printen van dossier naar hotfolder"
<# Start with only when hotfolder is empty #>
$hotfolderfiles = Get-ChildItem $Hotfolder -Filter *.pdf 
$hotfoldernumfiles = $hotfolderfiles.Count 
if ($hotfoldernumfiles -ne 0) {
[System.Windows.MessageBox]::Show('Hotfolder is niet, leeg hotfolder "' + 
$Hotfolder + '" en start hotfolderprint opnieuw','Waarschuwing','Ok')
exit
}
<# Set timer #>
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 2000
<# Processing each interval #>
Function WaitForStart {

<# $HotFolderLabel.Text = Get-Date -Format "HH:mm:ss"#>
Write-Host "Wait for Start " + $DossierFolderLabel.Text
$Logging.AppendText($HotFolderLabel.Text + $nl )
}
<# Start Timer on Load #>
Function Form_Load {
$timer.Start()
Write-Host "Form load"
}
<# Interval Handling #>
Function Timer_Tick {
$DossierFolderLabel.Text = Get-Date -Format "HH:mm:ssffff"
Write-Host "Timer Tick" + $DossierFolderLabel.Text
WaitForStart
}
$LocalPrinterForm.Add_Load({Form_Load})
$timer.add_tick({Timer_Tick})
[void]$LocalPrinterForm.ShowDialog()

关闭窗口时,计时器不会被破坏。因此,来自计时器的事件被排队。每次启动应用程序时,都会处理排队的事件,从而导致事件风暴。然后,每次启动应用程序后,都会启动一个额外的计时器。因此,以2000秒为间隔,每秒钟至少接收一个事件。

解决方案很简单;

在脚本终止时,添加$timer.Dispose()作为脚本中的最后一行作为catch-all。要在窗体以可控方式关闭时处理计时器,请添加一个功能来处理窗体的取消关闭事件

Function Form_Closing {
$timer.Stop();  
Write-Host "Form unload"
}

并注册处理Add_Closing事件的函数。

$LocalPrinterForm.Add_Closing({Form_Closing})

最新更新