【发布时间】:2022-01-27 04:57:41
【问题描述】:
我正在执行一个用 C++ 编写的旧控制台命令的调用表达式,并且我无法控制 C++ 源代码...但是,我认为它会捕获 Ctrl-C 以阻止命令行打断它...因此,当我从我的 powershell 脚本调用表达式时,无法使用 ctrl-C 中断执行,它会锁定我的终端,我需要继续杀死并重新启动我的终端窗口...这非常烦人...
在使用invoke-expression启动C++程序时,有没有办法确保powershell获取ctrl-C而不是C++程序? 就像也许,我可以拒绝将标准输入提供给 C++ 程序,而让 powershell 拥有它?或者也许有一些解决方案,标准输入转到另一个等待 ctrl-c 然后杀死调用表达式线程的 powershell 线程。..
例子:
PS> $cmd = <path_to_misbehaving_cpp_program_that_doesnt_Allow_ctrl_c_To_break_it>
PS> invoke-expression $cmd
# now here if I ctrl-C I can't break out of the invoke-expression....
# But I need this capability to ctrl-C
# to break the script and terminate the
# invoke expression of the C++ program.
【问题讨论】:
-
在 Powershell ISE 中是否可以使用停止按钮(红色方块)?
-
有趣的想法,但它需要从标准的 powershell 窗口运行......除非这能帮助您理解问题?
-
Start-Process做另一个线程并在需要时终止进程? -
您需要能够在应用程序输出时使用它,还是只需要在您按下 Ctrl+C 之前安静地运行?
-
顺便说一句:
Invoke-Expression(iex) should generally be avoided;绝对don't use it to invoke an external program or PowerShell script。如果您的可执行路径(仅)存储在$cmd中,只需使用& $cmd调用它
标签: powershell