Nivid Dholakia
Nivid Dholakia

Reputation: 5462

How can I get whether a process has executed properly or had some errors while executing?

I have a process that I need to start through WPF using C# as back end. The process is starting properly but in the process there is some error. In other words I can say that the process did not start properly. So how can I get that information on my code-behind?

For Example:

p.StartInfo.FileName = BasePath;
p.StartInfo.Arguments = args;
p.Start();

But after executing this file I am getting an error that some of the related DLLs are missing. I know the cause but if I have to detect this error, how could I get it on my code-behind?

Upvotes: 2

Views: 2412

Answers (1)

Rick Sladkey
Rick Sladkey

Reputation: 34250

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

Upvotes: 5

Related Questions