【发布时间】:2021-06-18 10:58:49
【问题描述】:
我使用的代码在独立时工作得很好,但是现在我把它放到一个按钮中它就停止了。 例如我有 400571 行,我在其中查找匹配项,进度条停在第 400000 行,因为我告诉他每 1000 行更新一次写入进度以提高性能。
我很确定关键点必须是 if($i % 1000 -eq 0) 因为如果我将它降低到 10 他会停在 400570 并且仍然没有完成,但我不想将它设置为 1,因为每行刷新进度条会占用更多时间。
按钮中包含的代码是:
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(30,10)
$OKButton.Size = New-Object System.Drawing.Size(300,92)
$OKButton.Text = "Filtern"
$OKButton.Name = "Filter"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::None
$OKButton.Add_Click({$i= 0
$path = "C:\temp\smtpfilter\LNS5filter.txt"
$length = (Get-Content $path).Length
#Datum, Hostname und Message Nummer
$result = Get-Content $path | ForEach-Object {
if($_ -match '(\d{2}\.\d{2}\.\d{4} \d{2}:\d{2}:\d{2}).*\(((?:\d{1,3}\.){3}\d{1,3})\) disconnected\.?\s+(\d+) message\[s\]'){
try {
#$dns = [System.Net.Dns]::GetHostEntry($matches[2]).HostName
}
catch {
#$dns = 'Not available'
}
[PsCustomObject]@{
IP = $matches[2]
Messages = [int]$matches[3]
#DNSName = $dns
Date = [datetime]::ParseExact($matches[1], 'dd.MM.yyyy HH:mm:ss', $null)
}
$i++
if($i % 1000 -eq 0){
Write-Progress -activity "Searching for matches" -status "Scanned: $i of $($length)" -percentComplete (($i / $length) * 100)
}
}}
#Messages Counted
$cumulative = $result | Group-Object -Property IP | ForEach-Object {
[PsCustomObject]@{
IP = $_.Name
Messages = ($_.Group | Measure-Object -Property Messages -Sum).Sum
#DNSName = $_.Group[0].DNSName
Date = ($_.Group | Sort-Object Date)[-1].Date
}
}})
$objForm.Controls.Add($OKButton)
(我 # dns 命令,因为我现在远程工作,解析名称需要很长时间)
【问题讨论】:
标签: powershell user-interface progress-bar