0

I'm trying to run this .exe file from my c# code, it does call the .exe file but then it crashes midway through. If I click on the .exe on the explorer it does its job, so I wonder if there's a problem with the code I'm using to invoke it:

            string fileName =  "loadscript.exe";
            Utils.Logger.Info("Calling script:" + fileName);
            Process process = new Process();
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.FileName = fileName;
            process.StartInfo.RedirectStandardOutput = true;
            process.Start();
            Thread.Sleep(10000);
            process.WaitForExit();
            int exitCode = process.ExitCode;
            string output = process.StandardOutput.ReadToEnd();
            Utils.Logger.Info(".exe Output: ");
            Utils.Logger.Info(output);
3
  • 1
    kind of looks like you're calling a .bat, not an .exe.. anyway: what kind of crash is this? What's the error? Commented Mar 30, 2011 at 15:48
  • Yes, you are correct. It's supposed to be an .exe instead of a .bat. My bad. It just bails and returns an exit code 9. Commented Mar 30, 2011 at 15:53
  • 1
    Argh, make your code snippet accurate! Commented Mar 30, 2011 at 15:57

2 Answers 2

5
  Thread.Sleep(10000);
  process.WaitForExit();
  int exitCode = process.ExitCode;
  string output = process.StandardOutput.ReadToEnd();

It seems to me like this is creating a deadlock and this might be the problem for the eventual crash. Remove the sleep and try this instead:

  string output = process.StandardOutput.ReadToEnd();
  process.WaitForExit();
  int exitCode = process.ExitCode;

Please see the answer to this question for an explanation:

ResGen.exe stucks when redirect output

Sign up to request clarification or add additional context in comments.

8 Comments

Do you mean that it should work if I just take the whole output out of the equation? I've already tried and nothing.
Yes, that should work. If you want to redirect the output you need to read it before you call WaitForExit()
See also the MSDN remarks for RedirectStandardOutput.
I tried both ways and nothing. In fact, I added the output part to see what came out after I found out that the call to the .exe didn't work. What I mean is that I got the error when I was just using the Start() and WaitForExit(). I do believe the problem is in the code though, but I don't know where.
Then I would say it's likely something in your exe fails. Are you sure the working directory is correct? Maybe try setting the WorkingDirectory property yourself in the StartInfo.
|
1
       process.StartInfo.UseShellExecute = false;

That requires you to specify the name of a .exe file. With it set to true, another Windows function is used to start the file, it is smart enough to figure out that a .bat file requires cmd.exe to be started to interpret the commands in the .bat file.

Which is what you need to do yourself now, the FileName must be "cmd.exe", the Arguments property needs to be "loadscript.bat".

1 Comment

This worked for me as is on Windows 7 - with UseShellExecute = false though I get an exception that the stream cannot be redirected - did this behavior change?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.