【问题标题】:Dismiss "blah.exe has stopped working" message. C#关闭“blah.exe 已停止工作”消息。 C#
【发布时间】:2015-02-16 04:01:18
【问题描述】:

我有一段 C# 代码,它调用一个进程,指向另一个可执行文件。在极少数情况下会发生访问冲突,后者会被操作系统终止,并显示消息“program.exe 已停止工作...... windows 可以在线检查解决方案,等等等等......”。我可以使用带有预定义超时的 WaitForExit 来终止和关闭进程,但是提到的窗口一直挂起。有可能以某种方式解雇它吗?

调用外部程序的代码如下:

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardError = true;
startInfo.FileName = pathToExe;
startInfo.ErrorDialog = false;
startInfo.Arguments = arguments;
string error = "";


using (Process exeProcess = System.Diagnostics.Process.Start(startInfo))
{
    try
    {
        if (!exeProcess.WaitForExit(timeout))
        {
            /// timed out
            /// clean up, set err message
            exeProcess.Kill();
            error = "timed out";
        }
        else if (exeProcess.ExitCode != 0)
        {
            /// aborted itself on error
            /// clean up, return err message
            error = exeProcess.StandardError.ReadToEnd();
        }
        else
        {
           //finished correctly
           //do some useful stuff
        }
    }
    catch(Exception e)
    {
        error = e.Message;
    }
}

【问题讨论】:

    标签: c# windows exe unmanaged terminate


    【解决方案1】:

    有一个 Windows API 调用 SetErrorMode 允许禁用此行为。但是,您不能为另一个进程设置错误模式。因此,此解决方案意味着您可以控制“blah.exe”源代码。关于C++C# 主题的有用答案。

    另一个建议的解决方案是将外部程序作为 Windows 服务运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      • 2020-02-03
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多