简体   繁体   English

如何抑制使用System.Diagnostic.Process执行的exe的对话框?

[英]How do I supress dialogs from an exe that is executed using System.Diagnostic.Process?

I'm using a System.Diagnostic.Process to call another .exe. 我正在使用System.Diagnostic.Process调用另一个.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. 该.exe有时会崩溃,并且弹出消息会停止整个过程-> [应用程序名称]遇到问题并被迫关闭。 I'm trying to find a way to make my c# program ignore this popup and continue executing. 我试图找到一种方法使我的C#程序忽略此弹出窗口并继续执行。

是否无法通过调用.exe来修复错误?

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: 订阅Process.Exited事件,然后检查Process.ExitCode:

 public void StartProcess()
{
 p.StartInfo.FileName = BasePath;
 p.StartInfo.Arguments = args;
 p.Start();
 p.Exited += new EventHandler(Process_Exited);
}

void Process_Exited(object sender, EventArgs e)
{
var p = sender as Process;
if (p.ExitCode != 0)
    MessageBox.Show(string.Format("Process failed: ExitCode = {0}", p.ExitCode));
}

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 the DEBUG_PROCESS flag in the process creation flags) and then have a thread process the debug events provided by WaitForDebugEvent ; 恕我直言,运行这样一个有问题的exe的最干净的选择是启动它,赋予它进程调试权限(您可能必须通过P / Invoke通过在进程创建标志中使用DEBUG_PROCESS标志显式调用CreateProcess ),然后使用线程进程WaitForDebugEvent提供的调试事件; 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 call ContinueDebugEvent to let the program run normally. 每当遇到未处理的最后机会异常时,您都可以通知主线程,终止子进程(从而避免使用默认的Windows异常处理程序),并在必要时重新启动它,在所有其他情况下,只需调用ContinueDebugEvent以使程序正常运行即可。

Notice that the pointer to the "event structure" provided by WaitForDebugEvent could be tricky to work with in C#. 注意,在C#中使用WaitForDebugEvent提供的指向“事件结构”的指针可能很棘手。


edit : fortunately it seems that someone made a nice managed wrapper for the native debugging API, see here . 编辑 :幸运的是,似乎有人为本地调试API做了一个不错的托管包装,请参见此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.Diagnostic.Process中的死锁 - Deadlock in System.Diagnostic.Process 无法使用System.Diagnostic.Process运行nbtstat - Unable to run nbtstat using System.Diagnostic.Process 如何知道是否已System.Diagnostic.Process由于退出超时? - How to know if a System.Diagnostic.Process has exited due to timeout? 我可以捕获用C#的System.Diagnostic.Process()启动的命令行应用程序的APPCRASH吗? - Can I catch APPCRASH of command line app launched with C#'s System.Diagnostic.Process()? 获取密码提示C#System.Diagnostic.Process的通知 - Getting notified of password prompt C# System.Diagnostic.Process 在C#中使用System.Diagnostic.Process及其UninstallString卸载程序 - Uninstall program using System.Diagnostic.Process and its UninstallString in C# 什么是WinRT(C#)上的System.Diagnostic.Process的等价物? - What's the equivalent of the System.Diagnostic.Process on WinRT (C#)? System.Diagnostic.Process.Start()是否正确运行.exe文件 - Does System.Diagnostic.Process.Start() runs a .exe file correctly 如何从 Process.Start 分离 exe - How do i detach an exe from Process.Start 使用System.Diagnostic Process时,我会错过进程开始和捕获输出开始之间的一些输出行吗? - When using System.Diagnostic Process, will I miss some output lines between the start of process and start of capturing output?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM