多线程/进度条应用(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 2014-09-17 11:07  momingliu11  阅读(1771)  评论(0编辑  收藏  举报