多线程/进度条应用(progressbar)
使用Control Sets 下的 ProgressBar - Responsive Loop控件
ProcessBar 或者 CancelBar 都可以被设置为 invisible
代码如下(分享自PowerShell群):
1 $OnLoadFormEvent = { 2 #TODO: Initialize Form Controls here 3 4 } 5 6 7 $buttonCancelProcess_Click = { 8 $script:CancelLoop = $true 9 } 10 11 $buttonStartProcess_Click = { 12 #Init CancelLoop 13 $script:CancelLoop = $false 14 $buttonCancelProcess.Enabled = $true 15 #Disable the button so we don't trigger it again 16 $this.Enabled = $false 17 #Reset the Progress Bar 18 $progressbar1.Value = 0 19 20 for ($i = 0; $i -lt $progressbar1.Maximum; $i++) 21 { 22 #---------------------------------------- 23 #Place custom script here 24 $richtextbox1.AppendText($i.ToString() + "`r`n") 25 sleep -Seconds 1 26 #---------------------------------------- 27 #process the pending message 28 [System.Windows.Forms.Application]::DoEvents() 29 30 if ($script:CancelLoop -eq $true) 31 { 32 #Clear the progress bar 33 $progressbar1.Value = 0 34 #Exit the loop 35 break; 36 } 37 #Step the progress bar 38 $progressbar1.PerformStep() 39 } 40 41 #Enable the button so we can click it again 42 $this.Enabled = $true 43 $buttonCancelProcess.Enabled = $false 44 } 45 46 $richtextbox1_TextChanged={ 47 #TODO: Place custom script here 48 $richtextbox1.ScrollToCaret() 49 } 50 51 $buttonRunProcess_Click={ 52 $buttonRunProcess.Enabled = $false 53 #TODO: Set the process path there 54 Add-ProcessTracker -FilePath "$env:windir/System32/notepad.exe" ` 55 -CompletedScript { 56 $buttonRunProcess.Enabled = $true 57 $buttonRunProcess.ImageIndex = -1 58 }` 59 -UpdateScript { 60 #Animate the Button 61 if($buttonRunProcess.ImageList -ne $null) 62 { 63 if($buttonRunProcess.ImageIndex -lt $buttonRunProcess.ImageList.Images.Count - 1) 64 { 65 $buttonRunProcess.ImageIndex += 1 66 } 67 else 68 { 69 $buttonRunProcess.ImageIndex = 0 70 } 71 } 72 } 73 }
进度条显示代码(同样是使用Control Sets 下的 ProgressBar - Responsive Loop控件),代码如下:
for ($i = 0; $i -lt 100; $i++)
{
$progressbar1.Minimum = 0
$progressbar1.Maximum = 99
$progressbar1.Value = $i
#Start-Sleep 1
}
分类:
PS Studio
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?