Solved

Process.Exitcode returns wrong value

Posted on 2013-11-15
6
511 Views
Last Modified: 2013-11-19
I've written a C# class that calls a DOS application in the context of a Process. The problem is that Process.Exitcode always returns 0 indicating success even if the DOS program fails. I've run this DOS application in a batch file from a command prompt, and I know it returns an error code. Here's my setup for the process:

        public void Start()
        {
            m_isDone = false;
            m_result = 0;
            m_isStandardOutputDone = false;
            m_isErrorOutputDone = false;
            m_consoleProcess = new Process();
            m_consoleProcess.StartInfo.FileName = m_command;
            m_consoleProcess.StartInfo.Arguments = m_commandArguments;
            m_consoleProcess.StartInfo.UseShellExecute = false;
            m_consoleProcess.StartInfo.CreateNoWindow = true;
            m_consoleProcess.StartInfo.RedirectStandardOutput = true;
            m_consoleProcess.StartInfo.RedirectStandardError = true;
            m_consoleProcess.OutputDataReceived += StandardOutputEventHandler;
            m_consoleProcess.ErrorDataReceived += ErrorOutputEventHandler;
            m_consoleProcess.Start();
            m_consoleProcess.BeginOutputReadLine();
            m_consoleProcess.BeginErrorReadLine();
        }

And the standard output event handler:

        private void StandardOutputEventHandler(object sendingProcess, DataReceivedEventArgs eventArgs)
        {
            String output = eventArgs.Data;
            if ( !String.IsNullOrEmpty(output) )
            {
                // Notify standard output event subscribers that there's new text.
                StandardOutputEvent(this, eventArgs);
            }
            else
            if ( (output == null) && m_consoleProcess.HasExited )
            {
                m_isStandardOutputDone = true;

                if ( m_isErrorOutputDone && !m_isDone )
                {
                    // Signal process completion.
                    m_isDone = true;
                    m_result = m_consoleProcess.Exitcode;
                    EventArgs e = new EventArgs();
                    if ( ProcessDoneEvent != null )
                    {
                        ProcessDoneEvent(this, e);
                    }
                }
            }
        }

It's here in the standard output event handler that I determine whether the process has completed and record the exit code. This works well, except that the exit code doesn't represent the correct output of the DOS program. How do I obtain the correct exit code?
0
Comment
Question by:alconlabs
  • 3
  • 2
6 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39653357
It doesn't look like you are waiting for the process to end, so the ExitCode won't be set yet.
0
 

Author Comment

by:alconlabs
ID: 39653940
m_consoleProcess.HasExited is true, so I assume the process has ended. The documentation for HasExited would seem to imply that if HasExited is true, it's safe to query the ExitCode and ExitTime properties, no?
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 39653964
As a test, you can try Process.WaitForExit, and see if the ExitCode is set.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 23

Expert Comment

by:Ioannis Paraskevopoulos
ID: 39654127
I think you could store the exit code of the DOS app, and just 'return' it from your main sub.
0
 

Author Comment

by:alconlabs
ID: 39661333
WaitForExit didn't return, and the event handler for the Exited event didn't fire. Turns out I need to set EnableRaisingEvents to get these to work (though OutputDataReceived and ErrorDataReceived events work just fine without it). I'll give the points to TheLearnedOne since he got me headed down the right path.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39661340
Aah, yes, the needle-in-the-haystack...glad you found it.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now