随笔 - 911  文章 - 5  评论 - 94  阅读 - 243万

多线程/进度条应用(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
}
posted on   momingliu11  阅读(1776)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示