Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

c# - catch another process unhandled exception

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...