如何禁止使用 System.Diagnostic.Process 执行的 exe 中的对话框?

发布于 2024-12-07 01:23:59 字数 164 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情愿 2024-12-14 01:23:59

修复调用 .exe 的错误不是一个选项吗?

Is fixing the error with the .exe being called not an option?

枉心 2024-12-14 01:23:59

在这种情况下,当您的进程无法正常工作时,您将看到一个窗口默认的弹出窗口,但如果您想知道该进程是否成功运行,那么此代码将为您工作

订阅 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));
}

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:

 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));
}
意中人 2024-12-14 01:23:59

恕我直言,运行这样一个有缺陷的 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 the DEBUG_PROCESS flag in the process creation flags) and then have a thread process the debug events provided by 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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文