[英]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.