首页 > 解决方案 > How to handle countdown in windows form using PowerShell?

问题描述

I would like to use countdown in my windows form. My windows form contains a button and some other tools. I have a problem once the countdown starts, the button and other tools are not clickable, because I use Show to start the countdown. But if I change to ShowDialog the countdown does not work, but the button and other tools in the form works.

    function JobHandlingAdd
    {
         #--[ Function ]--#
    
         # Job Handling Add Configuration
         function AddConfig
         {
              param (
    
                   $MainFontWidth, $WidthComboBoxSize, $ComboLocationSize, $LabelComboLocation, 
                   $ButtonRefreshHeight, $ButtonRefreshWidth, $GroupboxImageListWidth, $GroupboxImageListHeight,
                   $WidthLengthTitleLocationWidth, $TitleFontSizeWidth, $TitleLocationHeight, $WidthGroupBoxSWPOLocationSIze,
                   $GroupboxSWPOLocation, $CheckListBoxWidth, $CheckListBoxHeight, $CheckListBoxLocation, $GroupBoxExcecutionWidth, 
                   $GroupBoxExcecutionHeight, $ButtonCheckLocationWidth, $ButtonCheckLocationHeight, $ButtonCheckWidth, $ButtonCheckHeight, 
                   $ButtonCreateLocationWidth,$ButtonCreateLocationHeight, $ButtonCreateWidth, $ButtonCreateHeight, $PanelWidth, $PanelHeight          
              )
         
              # Main Form Configuration
              $MainFontSize = $Width / $MainFontWidth
              $MainFont = New-Object System.Drawing.Font("Segoe UI",$MainFontSize,[System.Drawing.FontStyle]::Regular,[System.Drawing.GraphicsUnit]::Pixel)
              $MainFormAdd.Font = $MainFont
              $MainFormAdd.AutoScaleMode = "Font"
    
              # ComboBox Image List Configuration
              $WidthComboBox = $Width / $WidthComboBoxSize
              $Combo.Size = New-Object System.Drawing.Size($WidthComboBox,$WidthComboBox)
              $Combo.Location = New-Object System.Drawing.Size(5,$ComboLocationSize)
              $LabelComboBox.Location = New-Object System.Drawing.Size(5,$LabelComboLocation) 
         
              # Button Refresh Image List Configuration
              $HeightButtonRefreshLocation = $Height / $ButtonRefreshHeight
              $WidthButtonRefreshLocation = $WidthComboBox / $ButtonRefreshWidth
              $ButtonRefresh.Location = New-Object System.Drawing.Size($WidthButtonRefreshLocation,$HeightButtonRefreshLocation)
              $ButtonRefresh.AutoSize = $true
         
              # GroupBox Image List Configuration
              $GroupboxImgaeList.Location = New-Object System.Drawing.Size($GroupboxImageListWidth,$GroupboxImageListHeight)
              $GroupboxImgaeList.Controls.AddRange(@($LabelComboBox, $Combo, $ButtonRefresh))
         
              # Title Configuration
              $WidthLengthTitleLocation = $Width / $WidthLengthTitleLocationWidth
              $TitleFontSize = $Width / $TitleFontSizeWidth
              $Title.Location = New-Object System.Drawing.Size($WidthLengthTitleLocation, $TitleLocationHeight) 
              $Title.AutoSize = $true
              $TitleFont = New-Object System.Drawing.Font("Segoe UI",$TitleFontSize,[System.Drawing.FontStyle]::Bold,[System.Drawing.GraphicsUnit]::Pixel)
              $Title.Font = $TitleFont
              $MainFormAdd.Controls.Add($Title)
         
              # GroupBox SWPO List Configuration
              $WidthGroupBoxSWPOLocation = $Width / $WidthGroupBoxSWPOLocationSIze
              $GroupboxSWPO.Location = New-Object System.Drawing.Size($WidthGroupBoxSWPOLocation,$GroupboxSWPOLocation)
              $GroupboxSWPO.Controls.AddRange(@($CheckedListBox))
         
              # CheckList SWPO Configuration
              $WidthCheckListBoxSize = $Width / $CheckListBoxWidth
              $HeightCheckListBoxSize = $Height / $CheckListBoxHeight 
              $WidthCheckListBoxLocation = $WidthCheckListBoxSize / $CheckListBoxLocation 
              $CheckedListBox.Location = New-Object System.Drawing.Size($WidthCheckListBoxLocation,50) 
              $CheckedListBox.Size = New-Object System.Drawing.Size($WidthGroupBoxSWPOLocation,$HeightCheckListBoxSize) 
         
              # GroupBox Execution Configuration
              $WidthGroupBoxExcecutionLocation = $Width / $GroupBoxExcecutionWidth 
              $GroupBoxExcecution.Location = New-Object System.Drawing.Size($WidthGroupBoxExcecutionLocation,$GroupBoxExcecutionHeight)
              $GroupBoxExcecution.Controls.AddRange(@($ButtonCheckExecution, $ButtonCreateExecution))
         
              # Button Check Execution Configuration
              $WidthButtonCheckLocation = $WidthGroupBoxExcecutionLocation / $ButtonCheckLocationWidth
              $HeightButtonCheckLocation = $Height / $ButtonCheckLocationHeight
              $ButtonCheckExecution.Location = New-Object System.Drawing.Size($WidthButtonCheckLocation,$HeightButtonCheckLocation)
              $WidthButtonCheckSize = $Width / $ButtonCheckWidth 
              $HeightButtonCheckSize = $Height / $ButtonCheckHeight 
              $ButtonCheckExecution.Size = New-Object System.Drawing.Size($WidthButtonCheckSize,$HeightButtonCheckSize)
         
              # Button Create Execution Configuration
              $WidthButtonCreateLocation = $WidthButtonCheckLocation + $ButtonCreateLocationWidth 
              $HeightButtonCreateLocation = $Height / $ButtonCreateLocationHeight 
              $ButtonCreateExecution.Location = New-Object System.Drawing.Size($WidthButtonCreateLocation,$HeightButtonCreateLocation) 
              $WidthButtonCreateSize = $Width / $ButtonCreateWidth 
              $HeightButtonCreateSize = $Height / $ButtonCreateHeight  
              $ButtonCreateExecution.Size = New-Object System.Drawing.Size($WidthButtonCreateSize,$HeightButtonCreateSIze)
         
              # PictureBox Configuration
              $PictureBox.SizeMode = "Autosize"
              $WidthPictureLocation = $Width / 180
              $HeightPictureLocation = $Height / 25
              $PictureBox.Location = New-object System.Drawing.Size($WidthPictureLocation,$HeightPictureLocation)
              
              # Panel Configuration
              $MainFormAdd.Controls.Add($Panel)
              $Panel.Controls.AddRange(@($PictureBox))
              $WidthPanelLocation = $Width / $PanelWidth
              $HeigthPanelLocation = $Height / $PanelHeight 
              $Panel.Location = New-Object System.Drawing.Size($WidthPanelLocation,$HeigthPanelLocation)
         }
    
         #--[ Main Script Job Handling Add Job ]--#
    
         #Start-Process "$PathHome\200911a.exe" -WindowStyle Maximized
    
    
         # Initialize Main Form 
         $MainFormAdd = New-Object System.Windows.Forms.Form    
         $MainFormAdd.MaximizeBox = $false
         $MainFormAdd.text = "BPS Image Automation"
         $MainFormAdd.FormBorderStyle = 'FixedDialog'
         $MainFormAdd.StartPosition = "CenterScreen"
         $MainFormAdd.BackColor = "$White"
         $MainFormAdd.WindowState = 'Maximized'
         $MainFormAdd.SizeGripStyle = 'Hide'
    
         # Initialize Label Image List
         $LabelComboBox = New-Object system.Windows.Forms.Label
         $LabelComboBox.text = "Please select the image:"
         $LabelComboBox.AutoSize = $true
    
         # Initialize Title Main Form
         $Title = New-Object system.Windows.Forms.Label
         $Title.text = "JOB HANDLING"
         $Title.ForeColor = "$Blue"
         $MainFormAdd.Controls.Add($Title)
    
         # Initialize ComboBox Image List 
         $Combo = New-Object system.Windows.Forms.ComboBox
         $Combo.AutoSize = $true
         $Combo.DropDownStyle = "DropDownList"
         $Combo.Items.AddRange(@(Get-ChildItem -File $PathFlag).BaseName)
         $SelectedFile = 
         {
              $ImageSelected = $Combo.SelectedItem
              $ImageName = $ImageSelected.Substring(3,11)
              $SWPOList = [collections.arraylist](Get-ChildItem $PathPO -File | Where-Object {$_ -like "*$ImageName*"})
              if(!($SWPOList))
              {
                   [System.Windows.Forms.MessageBox]::Show("SWPO with this image is not available.`nPlease select another image.","[Error]" , "OK", "Error")
              }
              else {
    
                   $CheckedListBox.DataSource = $SWPOList
                   $CheckedListBox.DisplayMember = 'Name'
              }
         }
         $Combo.add_SelectedIndexChanged($SelectedFile)
         $Combo.DisplayMember = 'Name'
    
         # Initialize Button Refresh
         $ButtonRefresh = New-Object system.Windows.Forms.Button
         $ButtonRefresh.BackColor = "$Blue"
         $ButtonRefresh.ForeColor = "$White"
         $ButtonRefresh.text = "Clear All"
         $ButtonRefresh.FlatStyle = 1
         $ButtonRefresh.Add_Click({
    
              $Combo.Items.Clear()    
              $Combo.Items.AddRange(@(Get-ChildItem -File $PathFlag).BaseName)
    
              $CheckedListBox.DataSource = $null
         })
    
         # Initialize GroupBox Image List
         $GroupboxImgaeList = New-Object system.Windows.Forms.Groupbox
         $GroupboxImgaeList.text = "Image List:"
         $GroupboxImgaeList.AutoSize = $true
         $MainFormAdd.Controls.Add($GroupboxImgaeList)
    
         # Initialize CheckedListBox SWPO
         $CheckedListBox = New-Object System.Windows.Forms.CheckedListbox
         $CheckedListBox.CheckOnClick = $true
         $CheckedListBox.Enabled = $true
         $CheckedListBox.BackColor = "$White"
         $MainFormAdd.Controls.Add($CheckedListBox)
    
         # Initialize GroupBox SWPO
         $GroupboxSWPO = New-Object system.Windows.Forms.Groupbox
         $GroupboxSWPO.text = "Software PO List:"
         $GroupboxSWPO.AutoSize = $true
         $MainFormAdd.Controls.Add($GroupboxSWPO)
    
         # Initialize GroupBox SWPO
         $GroupBoxExcecution = New-Object system.Windows.Forms.Groupbox
         $GroupBoxExcecution.text = "Execution:"
         $GroupBoxExcecution.AutoSize = $true
         $MainFormAdd.Controls.Add($GroupBoxExcecution)
    
         # Initialize Button Check Execution
         $ButtonCheckExecution = New-Object System.Windows.Forms.Button 
         $ButtonCheckExecution.Text = "CHECK" 
         $ButtonCheckExecution.BackColor = "$Blue"
         $ButtonCheckExecution.ForeColor = "$White"
         $ButtonCheckExecution.FlatStyle = 1
         $ButtonCheckExecution.Add_Click({
         
              # Selected Image Folder
              $ImageSelected = $Combo.SelectedItem
              if($null -eq $ImageSelected)
              {
              [System.Windows.Forms.MessageBox]::Show("You don't choose any SWPO.`nPlease choose SWPO.", "[Error]" , "OK", "Error")
              }
              elseif ($ImageSelected) 
              {
                   $Global:ImageName = $ImageSelected.Substring(3,11)
              }
                   # Check Job Folder
                   if (!(Test-Path -Path $PathOperationalJob\$ImageName))
                   {
                        # Create New Job Folder
                        New-Item -ItemType Directory -Path $PathOperationalJob\$ImageName -Force
                   }
    
                   $AllJobFilesPRO = Get-ChildItem -Path $PathOperationalJob\$ImageName\*.pro -File | ForEach-Object { $_.BaseName.Substring(45).replace('_', '') }
                   $AllJobFilesQUE = Get-ChildItem -Path $PathOperationalJob\$ImageName\*.que -File | ForEach-Object { $_.BaseName.Substring(21).replace('_', '') }
              
              # Selected SWPO
              $SelectedCheckListBox = $CheckedListBox.CheckedItems
              $SelectedCheckListBoxArray = @($SelectedCheckListBox)
              $SelectedSWPO = @($SelectedCheckListBoxArray) | ForEach-Object {$_.Name.Substring(2,13)}
    
              # Mapping The SWPO
              foreach ($SWPO in $SelectedSWPO)
              {
                   $JobFilePro = $AllJobFilesPRO.Where( { $_ -like "*$SWPO*" })
                   $JobFileQue = $AllJobFilesQUE.Where( { $_ -like "*$SWPO*" })
                   if ($JobFilePro.count -gt 0) 
                   {
                        [System.Windows.Forms.MessageBox]::Show("This SWPO: $($JobFilePro[0])`r`nNOT available to create.", "[Error]" , "OK", "Error")
                   }
                   elseif ($JobFileQue.count -gt 0)
                   {
                        [System.Windows.Forms.MessageBox]::Show("This SWPO: $($JobFileQue[0])`r`nNOT available to create.", "[Error]" , "OK", "Error")           
                   }
                   else {
    
                        [System.Windows.Forms.MessageBox]::Show("This SWPO: $SWPO`r`nAvailable to create.", "[Info]" , "OK", "Info")
                        
                        Add-Content -Path "$PathModificationODMIC\$SSIDUnit`_$SerialNumber`_$ImageName.add" -Value $SWPO -Force  
                   }
              }
         })
    
         # Initialize Button Create Execution
         $ButtonCreateExecution = New-Object System.Windows.Forms.Button 
         $ButtonCreateExecution.Text = "CREATE" 
         $ButtonCreateExecution.BackColor = "$Blue"
         $ButtonCreateExecution.ForeColor = "$White"
         $ButtonCreateExecution.FlatStyle = 1
         $ButtonCreateExecution.Add_Click({
    
              # Check The Button Readiness
              if (!(Test-Path -Path "$PathModificationODMIC\$SSIDUnit`_$SerialNumber`_$ImageName.add"))
              {
                   [System.Windows.Forms.MessageBox]::Show("Cannot crate the job.`n Please choose the SWPO.", "[Error]" , "OK", "Error")               
              }
              else {
    
                   # Filter The Duplicated
                   Get-Content "$PathModificationODMIC\$SSIDUnit`_$SerialNumber`_$ImageName.add" | Sort-Object -Unique | 
                   Set-Content "$PathModificationODMIC\$SSIDUnit`_$SerialNumber`_$ImageName`_$Date.add"
                   Remove-Item "$PathModificationODMIC\$SSIDUnit`_$SerialNumber`_$ImageName.add" -Force
    
                   # Mapping The Job File
                   $SWPOFiles = Get-Content -Path "$PathModificationODMIC\$SSIDUnit`_$SerialNumber`_$ImageName`_$Date.add" | ForEach-Object {$_.Insert(11, "_")}
              
                   foreach ($SWPOfile in $SWPOFiles)
                   {
                        try 
                        {
                             # Copy-Item -Path $PathOriginalJob\*$SWPOfile*.job $PathOperationalJob\$ImageName -ErrorAction Stop
                             # Get-ChildItem -Path "$PathOperationalJob\$ImageName\*$SWPOfile*.job" | Rename-Item -NewName {"$Date`_" + $_.Name.Replace('.job','.que')}
                             if (Test-Path -Path $PathOperationalJob\$ImageName\*$SWPOfile*)
                             {                              
                                  $Message = "Creating job file successfully.`nClick 'OK' to continue the process."
                                  $MessageCode = 0
                             }
                             else {
    
                                  $Message = "Creating job file fail.`nPlease try again!"
                                  $MessageCode = 1
                             }
    
                             # if (Test-Path -Path "$PathModificationODMIC\$SerialNumber`_$ImageName`_$Date.add")
                             # {
                             #      #Remove-Item -Path $PathHome\SWPO.txt -Force
                             # }
                        }
                        catch 
                        {
                             $Message = "Creating job file fail.`n`nErrorMessage: $($_[0])"
                             $MessageCode = 1
                        }               
                   }
    
                   # Return Message
                   if ($MessageCode -eq 1)
                   {
                        [System.Windows.Forms.MessageBox]::Show("$Message", "[Message]" , "OK", "Error")
                   }
                   elseif ($MessageCode -eq 0)
                   {
                        $Respon = [System.Windows.Forms.MessageBox]::Show("$Message", "[Message]" , "OK", "Info")
                        if ($Respon -eq "OK")
                        {
                             $Message = "Click 'OK' to reboot the unit."
                             $Respon = [System.Windows.Forms.MessageBox]::Show("$Message", "[Message]" , "OK", "Info")
    
                             Write-Host "Reboot"
                             $MainFormAdd.Dispose()
                             Restart-Computer
                             
                        }
                   }          
              }
         })
    
    
         # Initialize PictureBox
         [reflection.assembly]::LoadWithPartialName("System.Windows.Forms")
         $GetPicture = (get-item "$PathHome\HPLogo.png")
         $InitPicture = [System.Drawing.Image]::Fromfile($GetPicture)
         $PictureBox = New-Object Windows.Forms.PictureBox
         $PictureBox.Image = $InitPicture
    
         # Initialize Panel
         $Panel = New-Object System.Windows.Forms.Panel
         $Panel.AutoSize = $true
    
         # Windows Screen Configuration
         $WidthAll = [System.Windows.Forms.Screen]::AllScreens.bounds.width
         $HeightAll = [System.Windows.Forms.Screen]::AllScreens.bounds.Height
         $Height = $HeightAll[0]
         $Width = $WidthAll[0]
    
         if($Height -le "768"){
    
              #testing
              AddConfig "96" "3.6" "50" "30" "1.26" "2.5" "20" "70" "2.4" "50" "20" "3" "70" "3.3" "1.29" "90" "1.42" "70" "170" "20" "9" "9.2" "150" "20" "9" "9.2" "1.4" "2.1"
         }
         elseif ($Height -gt "768" -and $Height -lt "992"){
    
              #testing
              AddConfig "96" "3.6" "100" "50" "1.26" "2.5" "20" "90" "2.4" "50" "20" "3" "90" "3.3" "1.29" "90" "1.4" "90" "170" "20" "9" "9.2" "300" "20" "9" "9.2"
         }
         elseif ($Height -gt "992" -and $Height -lt "1200") {
    
              AddConfig "96" "3.6" "100" "50" "1.26" "2.5" "20" "90" "2.4" "50" "20" "3" "90" "3.3" "1.29" "90" "1.4" "90" "170" "20" "9" "9.2" "300" "20" "9" "9.2" "1.3" "2.1"
         }
         elseif ($Height -gt "1200") {
    
              #testing
              AddConfig "96" "3.6" "100" "50" "1.26" "2.5" "20" "90" "2.4" "50" "20" "3" "90" "3.3" "1.29" "90" "1.4" "90" "170" "20" "9" "9.2" "300" "20" "9" "9.2"
         }
         else {
    
              Write-Host "Cannot define the height"
              AddConfig "96" "3.6" "100" "50" "1.26" "2.5" "20" "90" "2.4" "50" "20" "3" "90" "3.3" "1.29" "90" "1.4" "90" "170" "20" "9" "9.2" "300" "20" "9" "9.2"
         }
    
         $delay = 5
         $Counter_Label = New-Object System.Windows.Forms.Label
         $Counter_Label.AutoSize = $true
         $Counter_Label.ForeColor = "Green"
         $normalfont = New-Object System.Drawing.Font("Times New Roman",14)
         $Counter_Label.Font = $normalfont
         $Counter_Label.Left = 20
         $Counter_Label.Top = 20
         $MainFormAdd.Controls.Add($Counter_Label)
    
         while ($delay -ge 0)
         {
              $Counter_Label.Text = "Seconds Remaining: $($delay)"
              [void] $MainFormAdd.Show()
    
         start-sleep 1
         $delay -= 1
    
         }
         $MainFormAdd.Dispose()
       
    }
    
    $PathHome = "D:\29a"
    $PathFlag = "D:\29a\In"
    $PathPO = "D:\29a\source"
    $PathOperationalJob = "D:\29a\Process"
    $PathOriginalJob = "D:\29a\Init"
    $SerialNumber = "1234567891"
    $Image = "11XXR3DT666"
    $PathModificationODMIC = "D:\29a\Modif\ODC"
    
    $Date = (Get-Date).ToUniversalTime().ToString('yyyyMMddHHmmss')
    $SSIDUnit = "8888"
    JobHandlingAdd

标签: powershelltimercountershowdialog

解决方案


推荐阅读