私は、ユーザーがネットワーク プリンターをインストールできるようにする 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 ウィンドウを閉じる方法を知っている人はいますか?