You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell脚本中途终止失效:WinForms取消按钮点击无响应

PowerShell WinForms取消按钮Exit无效的解决方法
  • 直接替换事件里的exit为[Environment]::Exit(0),修改后的按钮代码:
$connectcancelButton = New-Object System.Windows.Forms.Button
$connectcancelButton.Location = New-Object System.Drawing.Point(197.5, 225)
$connectcancelButton.Size = New-Object System.Drawing.Size(75,25)
$connectcancelButton.Text = 'Abbrechen'
$connectcancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$connectcancelButton.Add_Click({
    [Environment]::Exit(0)
})
$connectform.Controls.Add($connectcancelButton)
  • 原因说明:在WinForms事件处理的脚本块中,exit仅会退出当前事件的执行上下文,不会终止整个PowerShell脚本的运行。而[Environment]::Exit(0)是CLR层面的退出命令,会直接终止整个PowerShell进程,脚本会立刻停止执行,无需添加贯穿全局的if判断。

内容的提问来源于stack exchange,提问作者cosmo_

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 07:36:34