2017-10-18 7 views
0

Ich versuche exit auszuführen, nachdem das Formular zu schließen, wenn „NEIN“ klicken, aber exit verursacht einen SystemfehlerAusstieg aus Powershell-Skript verursacht Unbehandelte Ausnahme

Code:

function No { 
      $Form.close() 
      exit 
    } 
$NoButton.Add_Click({No}) 

ohne Ausgang, es die Form wird geschlossen, aber es weiterhin den Skript

Systemfehler ausführt:

Unhandled exception has occured in a component in your application. If you click Continue, the application will ignore this error and attempt to continue.

System error.

Voll Button-Code:

function No { 
    $Form.close() 
    exit 
        } #end No 
# No button 
$NoButton = New-Object System.Windows.Forms.Button 
$NoButton.Location = New-Object System.Drawing.Size(95,80) 
$NoButton.Size = New-Object System.Drawing.Size(80,20) 
$NoButton.Text = "No" 
$NoButton.Add_Click({No}) 
$NoButton.Cursor = [System.Windows.Forms.Cursors]::Hand 
$Form.Controls.Add($NoButton) 
+0

ich diesen Fehler nicht reproduzieren kann, wenn mit '[System. Windows.Forms.Application] :: Run ($ Form) 'um das Fenster anzuzeigen - könnten Sie bitte Code anzeigen, der notwendig ist, um das Problem zu demonstrieren? –

Antwort

0

Sie müssen bis zum $Form warten tatsächlich geschlossen ist, und dann besser töten seinen eigenen Prozess:

$NoButton = New-Object System.Windows.Forms.Button 
$NoButton.Location = New-Object System.Drawing.Size(95,80) 
$NoButton.Size = New-Object System.Drawing.Size(80,20) 
$NoButton.Text = "No" 
$NoButton.Add_Click({$Form.close()}) 
$NoButton.Cursor = [System.Windows.Forms.Cursors]::Hand 
$Form.Controls.Add($NoButton) 
$Form.Add_Closed({Stop-Process -ID $PID}) # Kill it's own process when the form is closed 
Verwandte Themen