You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell脚本生成的进度条窗口存在但无法查看如何解决

问题根因

  • 写死的窗体坐标大概率超出当前显示器的可视工作区,导致窗体渲染到屏幕外
  • PowerShell单线程执行逻辑时,脚本的传输任务阻塞了WinForm的UI消息循环,窗体无法正常渲染、响应用户交互

修复步骤

1. 调整窗体定位逻辑

不要写死Location属性,改为自动居中到主屏幕,避免坐标溢出:
把原来的代码段:

$form_bar.StartPosition = "manual"
$form_bar.Location = '1320,840'

替换为:

# 自动居中到主屏幕,避免坐标溢出
$form_bar.StartPosition = "CenterScreen"
# 如果必须使用固定位置,可先校验坐标是否在屏幕工作区内再设置
# $workingArea = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea
# if(1320 -lt $workingArea.Width -and 840 -lt $workingArea.Height){
#     $form_bar.Location = [System.Drawing.Point]::new(1320,840)
# } else {
#     $form_bar.StartPosition = "CenterScreen"
# }

2. 补充UI消息处理逻辑

每次更新进度条之后调用[System.Windows.Forms.Application]::DoEvents(),处理UI消息队列,保证窗体可以正常渲染和响应操作,修改后的进度更新逻辑如下:

$percent = ($transferred_bytes / $total_bytes)*100
$formatted = '{0:0.0}' -f $percent
[int32]$progress = $percent
$CurrentTime = $Time.Elapsed
$estimated = [int]((($CurrentTime.TotalSeconds/$percent) * (100 - $percent)) / 60)
$label.Text = "Progress: $formatted% - $estimated mins to end"
if ($progress -ge 100) {
    $bar.Value = 100
} else {
    $bar.Value = $progress
}
$form_bar.Refresh()
# 新增此行,处理待响应的UI消息
[System.Windows.Forms.Application]::DoEvents()

其他注意点

你当前代码中$trasferred_bytes存在拼写错误,实际使用时请确认变量名和你定义的传输字节变量名一致,避免出现百分比计算异常。

内容的提问来源于stack exchange,提问作者Dario Corrada

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.15 01:15:03