如何禁止使用 System.Diagnostic.Process 执行的 exe 中的对话框?
我正在使用 System.Diagnostic.Process 来调用另一个 .exe。我正在创建一个应用程序,其目的是在远程服务器上运行。该 .exe 有时会崩溃,并且会弹出一条消息,停止整个进程 -> [应用程序名称]遇到问题,被迫关闭。我正在尝试找到一种方法让我的 c# 程序忽略此弹出窗口并继续执行。
I'm using a System.Diagnostic.Process to call another .exe. I'm creating an application which purpose is to run on a remote server. This .exe sometimes crashes and there's a message popup stopping the whole process -> [Application Name] has encountered a problem and is forced to close. I'm trying to find a way to make my c# program ignore this popup and continue executing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
修复调用 .exe 的错误不是一个选项吗?
Is fixing the error with the .exe being called not an option?
在这种情况下,当您的进程无法正常工作时,您将看到一个窗口默认的弹出窗口,但如果您想知道该进程是否成功运行,那么此代码将为您工作
订阅 Process.Exited 事件,然后检查 Process.ExitCode :
In that case when your process does not work properly you will see a popup thats windows default but if you want to know that process ran successfully or not then this code will work for you
Subscribe to the Process.Exited event and then check Process.ExitCode:
恕我直言,运行这样一个有缺陷的 exe 的最干净的选择是启动它,为您的进程授予对其的调试权限(您可能必须通过 P/Invoke 使用
DEBUG_PROCESS
显式调用CreateProcess
> 进程创建标志中的标志),然后让线程处理WaitForDebugEvent
;每当您遇到未处理的最后机会异常时,您可以通知主线程,终止子进程(从而避免默认的 Windows 异常处理程序)并在必要时重新启动它,在所有其他情况下只需调用ContinueDebugEvent
让程序正常运行。请注意,在 C# 中使用
WaitForDebugEvent
提供的指向“事件结构”的指针可能会很棘手。编辑:幸运的是,似乎有人为本机调试 API 制作了一个很好的托管包装器,请参阅 此处。
IMHO the cleanest option to run such a buggy exe is to start it giving to your process debug privileges over it (you probably have to call explicitly
CreateProcess
via P/Invoke with theDEBUG_PROCESS
flag in the process creation flags) and then have a thread process the debug events provided byWaitForDebugEvent
; whenever you get an unhandled, last-chance exception you can notify the main thread, kill the child process (thus avoiding the default Windows exception handler) and restart it if necessary, in all other cases just callContinueDebugEvent
to let the program run normally.Notice that the pointer to the "event structure" provided by
WaitForDebugEvent
could be tricky to work with in C#.edit: fortunately it seems that someone made a nice managed wrapper for the native debugging API, see here.