从powershell脚本退出会导致未处理的异常 [英] exit from powershell script causes Unhandled exception

查看:211
本文介绍了从powershell脚本退出会导致未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在单击否"按钮后关闭表单后尝试执行exit,但是exit会导致系统错误

I am trying to execute exit after closing the form when clicking "NO" button, but exit causes a system error

代码:

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

在没有退出的情况下,它将关闭表单,但会继续执行脚本

Without exit, it closes the form but it continues executing the script

系统错误:

应用程序中的组件发生未处理的异常.如果单击继续,应用程序将忽略此错误,并且 尝试继续.

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

系统错误.

完整按钮代码:

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) 

推荐答案

您需要等待$Form实际上关闭,然后更好地杀死它自己的进程:

You need to wait till the $Form is actually closed and then better kill it's own process:

$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

这篇关于从powershell脚本退出会导致未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆