gpt4 book ai didi

user-interface - 解除由启动任务控制的Powershell表单

转载 作者:行者123 更新时间:2023-12-02 23:15:57 28 4
gpt4 key购买 nike

我的任务是使用GUI构建Powershell脚本,该脚本使用户能够安装网络打印机。我已经成功地做到了,但是我不能满足在安装打印机时向用户显示“请稍候”窗口的要求。如果从主线程切换到窗口,则GUI挂起。如果我将显示窗口移到其他任务上,则永远无法再次关闭该窗口。这是我的尝试:

$waitForm = New-Object 'System.Windows.Forms.Form'

$CloseButton_Click={

# open "please wait form"
Start-Job -Name waitJob -ScriptBlock $callWork -ArgumentList $waitForm

#perform long-running (duration unknown) task of adding several network printers here
$max = 5
foreach ($i in $(1..$max)){
sleep 1 # lock up the thread for a second at a time
}

# close the wait form - doesn't work. neither does remove-job
$waitForm.Close()
Remove-Job -Name waitJob -Force
}

$callWork ={

param $waitForm

[void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
$waitForm = New-Object 'System.Windows.Forms.Form'

$labelInstallingPrintersPl = New-Object 'System.Windows.Forms.Label'
$waitForm.Controls.Add($labelInstallingPrintersPl)
$waitForm.ClientSize = '502, 103'
$labelInstallingPrintersPl.Location = '25, 28'
$labelInstallingPrintersPl.Text = "Installing printers - please wait..."

$waitForm.ShowDialog($this)
}

有人知道长时间运行的任务结束后如何关闭$ waitForm窗口吗?

最佳答案

您可以尝试在主线程上运行Windows窗体对话框,并在后台作业中执行实际工作:

Add-Type -Assembly System.Windows.Forms

$waitForm = New-Object 'System.Windows.Forms.Form'
$labelInstallingPrintersPl = New-Object 'System.Windows.Forms.Label'
$waitForm.Controls.Add($labelInstallingPrintersPl)
$waitForm.ClientSize = '502, 103'
$labelInstallingPrintersPl.Location = '25, 28'
$labelInstallingPrintersPl.Text = "Installing printers - please wait..."
$waitForm.ShowDialog($this)

Start-Job -ScriptBlock $addPrinters | Wait-Job

$waitForm.Close()

$addPrinters = {
$max = 5
foreach ($i in $(1..$max)) {
sleep 1 # lock up the thread for a second at a time
}
}

关于user-interface - 解除由启动任务控制的Powershell表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9427391/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com