如何使用PowerShell在Windows窗体中处理倒数计时?

如何解决如何使用PowerShell在Windows窗体中处理倒数计时?

我想在Windows窗体中使用倒数计时。我的Windows窗体包含一个按钮和一些其他工具。 倒数开始后,我就遇到了问题,按钮和其他工具不可单击,因为我使用Show来开始倒数。但是,如果我更改为ShowDialog,则倒数计时将不起作用,但是表单中的按钮和其他工具将起作用。

    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")
              }
              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")
                   }
                   elseif ($JobFileQue.count -gt 0)
                   {
                        [System.Windows.Forms.MessageBox]::Show("This SWPO: $($JobFileQue[0])`r`nNOT available to create.","Error")           
                   }
                   else {
    
                        [System.Windows.Forms.MessageBox]::Show("This SWPO: $SWPO`r`nAvailable to create.","[Info]","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")               
              }
              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]","Error")
                   }
                   elseif ($MessageCode -eq 0)
                   {
                        $Respon = [System.Windows.Forms.MessageBox]::Show("$Message","Info")
                        if ($Respon -eq "OK")
                        {
                             $Message = "Click 'OK' to reboot the unit."
                             $Respon = [System.Windows.Forms.MessageBox]::Show("$Message","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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-