2

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

4

3 に答える 3

2

メイン スレッドで 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
    }
}
于 2012-02-24T10:47:50.270 に答える
2

この最初の答えは正しかったです。メイン スレッドでフォームを作成し、長時間実行されるタスクを別のスレッドで実行します。フォームが閉じられるまでメイン コードを実行しない理由は、フォームの 'ShowDialog' メソッドを使用しているためです。このメソッドは、フォームが閉じられるまで後続のコードの実行を停止します。

代わりに「show」メソッドを使用してください。コードの実行は続行されます。おそらく、フォームを破棄するイベント ハンドラをいくつか含める必要があります。

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.Add_FormClosed({
$labelInstallingPrintersPl.Dispose()
$waitForm.Dispose()
})

$waitForm.Show($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
    }
}
于 2012-08-22T03:07:19.487 に答える