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);
animuson
  • 53,861
  • 28
  • 137
  • 147
hikizume
  • 578
  • 11
  • 25

2 Answers2

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

Community
  • 1
  • 1
Can Gencer
  • 8,822
  • 5
  • 33
  • 52
  • Do you mean that it should work if I just take the whole output out of the equation? I've already tried and nothing. – hikizume Mar 30 '11 at 16:04
  • Yes, that should work. If you want to redirect the output you need to read it before you call WaitForExit() – Can Gencer Mar 30 '11 at 16:08
  • See also the MSDN remarks for [RedirectStandardOutput](http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx). – ladenedge Mar 30 '11 at 16:16
  • 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. – hikizume Mar 30 '11 at 16:19
  • 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. – Can Gencer Mar 30 '11 at 16:31
  • The directory is correct. As I mentioned, the .exe is invoked but crashes midway through during execution. The code you suggested is supposed to work. I made a test application that calls notepad.exe and it works with that, but doesn't when I call my .exe file. Also, if I just click on my .exe file, it does what is supposed to do, so I don't supposed there's a problem with the .exe itself. – hikizume Mar 31 '11 at 07:29
  • Any chance you can see what the .exe is doing? – Can Gencer Mar 31 '11 at 08:40
  • Yes, it's a perl script for which I have the code. The problem was in the working directory on my code. That's why it would work on debug mode but not on release, on release the working directory took the value c:\Windows\System32. Switched to the folder where the service is installed and voila!. I still had to implement your code though so I think marking this as an answer is in order. thanx for the help! – hikizume Mar 31 '11 at 09:39
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".

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 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? – BrokenGlass Mar 30 '11 at 15:54