多线程/进度条应用(progressbar)

时间:2023-03-09 02:12:31
多线程/进度条应用(progressbar)

使用Control Sets 下的 ProgressBar - Responsive Loop控件

多线程/进度条应用(progressbar)

ProcessBar 或者 CancelBar 都可以被设置为 invisible

代码如下(分享自PowerShell群):

 $OnLoadFormEvent = {
#TODO: Initialize Form Controls here } $buttonCancelProcess_Click = {
$script:CancelLoop = $true
} $buttonStartProcess_Click = {
#Init CancelLoop
$script:CancelLoop = $false
$buttonCancelProcess.Enabled = $true
#Disable the button so we don't trigger it again
$this.Enabled = $false
#Reset the Progress Bar
$progressbar1.Value = 0 for ($i = 0; $i -lt $progressbar1.Maximum; $i++)
{
#----------------------------------------
#Place custom script here
$richtextbox1.AppendText($i.ToString() + "`r`n")
sleep -Seconds 1
#----------------------------------------
#process the pending message
[System.Windows.Forms.Application]::DoEvents() if ($script:CancelLoop -eq $true)
{
#Clear the progress bar
$progressbar1.Value = 0
#Exit the loop
break;
}
#Step the progress bar
$progressbar1.PerformStep()
} #Enable the button so we can click it again
$this.Enabled = $true
$buttonCancelProcess.Enabled = $false
} $richtextbox1_TextChanged={
#TODO: Place custom script here
$richtextbox1.ScrollToCaret()
} $buttonRunProcess_Click={
$buttonRunProcess.Enabled = $false
#TODO: Set the process path there
Add-ProcessTracker -FilePath "$env:windir/System32/notepad.exe" `
-CompletedScript {
$buttonRunProcess.Enabled = $true
$buttonRunProcess.ImageIndex = -1
}`
-UpdateScript {
#Animate the Button
if($buttonRunProcess.ImageList -ne $null)
{
if($buttonRunProcess.ImageIndex -lt $buttonRunProcess.ImageList.Images.Count - 1)
{
$buttonRunProcess.ImageIndex += 1
}
else
{
$buttonRunProcess.ImageIndex = 0
}
}
}
}

进度条显示代码(同样是使用Control Sets 下的 ProgressBar - Responsive Loop控件),代码如下:

for ($i = 0; $i -lt 100; $i++)
{
$progressbar1.Minimum = 0
$progressbar1.Maximum = 99
$progressbar1.Value = $i
#Start-Sleep 1
}