捕获另一个进程未处理的异常

发布于 2024-08-21 15:26:33 字数 554 浏览 8 评论 0原文

我想知道我是否可以捕获我开始使用 Process.Start(...) 的另一个进程抛出的未处理的异常

我知道我可以使用这个 链接 ,但我想要什么是捕获.net环境的Just In Time调试器通常捕获的错误,窗口中有如下文字: “您的应用程序中发生了未处理的异常。如果您继续,应用程序将忽略此错误并尝试继续。如果您单击“退出”,应用程序将立即关闭......” 然后是异常消息以及“继续”和“退出”按钮。

I wish to know if i can catch the unhandled exceptions thrown by another process which I started using the Process.Start(...)

I know i can catch the standered error using this link , but what I want is to catch the error that are usually caught by the Just In Time debugger of the.net environment, the window with the following words:
"An unhandled exception has occurred in your application . If you Continue, the application will ignore this error and attempt to continue . If you click Quit, the application will be shut down immediately ...."
Which is then followed by the exception message and a "Continue" and "Quit" button.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

溺ぐ爱和你が 2024-08-28 15:26:33

您可以尝试类似的方法来避免出现调试器问题,您不会得到异常,而只会得到退出代码:

class Program
{
    static void Main(string[] args)
    {
        try
        {
            ProcessStartInfo info = 
                 new ProcessStartInfo("ErroneusApp.exe");
            info.ErrorDialog = false;
            info.RedirectStandardError = true;
            info.RedirectStandardOutput = true;
            info.CreateNoWindow = true;
            info.UseShellExecute = false;

            System.Diagnostics.Process p = 
                System.Diagnostics.Process.Start(info);
            p.EnableRaisingEvents = true;
            p.Exited += p_Exited;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        Console.ReadLine();
    }


    static void p_Exited(object sender, EventArgs e)
    {
        Process p = sender as Process;
        if (p != null)
        {
            Console.WriteLine("Exited with code:{0} ", p.ExitCode);
        }
        else
            Console.WriteLine("exited");
    }

}

这个问题他们为此提供了另一种解决方法,但更改了一些注册表值。

You can try something like that to avoid the debugger question to appear, you won't get the exception but only the exit code:

class Program
{
    static void Main(string[] args)
    {
        try
        {
            ProcessStartInfo info = 
                 new ProcessStartInfo("ErroneusApp.exe");
            info.ErrorDialog = false;
            info.RedirectStandardError = true;
            info.RedirectStandardOutput = true;
            info.CreateNoWindow = true;
            info.UseShellExecute = false;

            System.Diagnostics.Process p = 
                System.Diagnostics.Process.Start(info);
            p.EnableRaisingEvents = true;
            p.Exited += p_Exited;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        Console.ReadLine();
    }


    static void p_Exited(object sender, EventArgs e)
    {
        Process p = sender as Process;
        if (p != null)
        {
            Console.WriteLine("Exited with code:{0} ", p.ExitCode);
        }
        else
            Console.WriteLine("exited");
    }

}

In this question they provided another workaround for that, but changing some registry values.

高速公鹿 2024-08-28 15:26:33

如果您正在调用 .Net 可执行程序集,您可以加载它并(由您自己承担风险 :D )将 Program 类的 Main 方法调用到 try_catch 语句中:

Assembly assembly = Assembly.LoadFrom("ErroneusApp.exe");
Type[] types= assembly.GetTypes();
foreach (Type t in types)
{
 MethodInfo method = t.GetMethod("Main",
     BindingFlags.Static | BindingFlags.NonPublic);
 if (method != null)
 {
    try
    {
        method.Invoke(null, null);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    break;
 }
}

但请注意这样做引入的安全风险。

If you are calling to a .Net executable assembly you can load it and (at your own risk :D ) call to the Main method of the Program class into a try_catch statement:

Assembly assembly = Assembly.LoadFrom("ErroneusApp.exe");
Type[] types= assembly.GetTypes();
foreach (Type t in types)
{
 MethodInfo method = t.GetMethod("Main",
     BindingFlags.Static | BindingFlags.NonPublic);
 if (method != null)
 {
    try
    {
        method.Invoke(null, null);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
    break;
 }
}

But be aware of the security risks you are introducing doing that.

时光是把杀猪刀 2024-08-28 15:26:33

不会。如果受控应用程序使用 standardError 和返回代码,您可能会收到发生错误或异常的通知,但您不能以任何方式捕获它。

No. If the controlled app utilizes standardError and return codes you may be notified of the occurance of an error or exception but you cannot trap it in any way.

带刺的爱情 2024-08-28 15:26:33

您可以简单地在新域中执行程序集,而不是像 jmservera 的答案那样在程序集周围潜行以尝试找到 Main 方法。请参阅这篇 msdn 文章

Instead of doing the whole sneaking around the assembly to try to find the Main method like in jmservera's answer, you can simply execute the assembly in a new domain. See this msdn article

轮廓§ 2024-08-28 15:26:33

在解决之前的目标调用问题时,最后我采用了以下方法。

您的某些应用程序在当前目录中具有依赖项,只要在另一个目录中执行它就会导致异常,因为依赖项不匹配。

Assembly assembly = Assembly.LoadFrom(file);
                    Directory.SetCurrentDirectory(Path.GetDirectoryName(file));
                    Type[] types = assembly.GetTypes();
                    foreach (Type t in types)
                    {
                        MethodInfo method = t.GetMethod("Main", BindingFlags.Static | BindingFlags.NonPublic);
                        if (method != null)
                        {
                            try
                            {
                              method.Invoke(null, null);
                            }

Directory.SetCurrentDirectory(Path.GetDirectoryName(文件)); //通过引用当前目录目标调用异常将得到解决。

In resolving to the previous target invocation issue, finally i end up with the below method.

Some of your application would have dependencies in the current directory, which leads to exception whenever it is executed in the another directory since the dependencies doesnt match.

Assembly assembly = Assembly.LoadFrom(file);
                    Directory.SetCurrentDirectory(Path.GetDirectoryName(file));
                    Type[] types = assembly.GetTypes();
                    foreach (Type t in types)
                    {
                        MethodInfo method = t.GetMethod("Main", BindingFlags.Static | BindingFlags.NonPublic);
                        if (method != null)
                        {
                            try
                            {
                              method.Invoke(null, null);
                            }

Directory.SetCurrentDirectory(Path.GetDirectoryName(file)); //by refering to the current directory target invocation exception would be resolved.

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