0

I am executing a .exe that sits on a remote server however PsExec seems to hang and the local service does not exit. The .exe does run successfully on the server however when it is finished I want the local service to exit.

This is the code I have atm:

Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = @"C:\Program Files (x86)\PSTools\PsExec.exe";
p.StartInfo.Arguments = @"\\<remote .exe path>";
p.Start();

//I have tried the following to exit the local service when a response is
//received but it still hangs.
//string output = p.StandardOutput.ReadToEnd();
//string errormessage = p.StandardError.ReadToEnd();
//p.WaitForExit();

What do I need to do to exit the local service when the remote service has finished?

Jack Eker
  • 169
  • 2
  • 2
  • 12

2 Answers2

0

Did you try p.WaitForInputIdle(); ?

------------- UPDATE

Difference GUI/non-GUI: How can I tell if a process has a graphical interface?

Community
  • 1
  • 1
varocarbas
  • 12,354
  • 4
  • 26
  • 37
  • I hadn't, but I just did and I got the error "WaitForInputIdle failed. This could be because the process does not have a graphical interface." The remote .exe just kicks off a console to run some operations. Thank you for the suggestion :) – Jack Eker Jun 28 '13 at 14:41
  • Certainly curious error (never seen before). But see what I found when I type it in google http://stackoverflow.com/questions/3785698/how-can-i-tell-if-a-process-has-a-graphical-interface it seems like a solution for your problem: one of the answers gives options to track if the process has exited for both cases with and without GUI. – varocarbas Jun 28 '13 at 14:44
0

You could try running psexec with the -d option, which is recommended for non-interactive applications. Or you could maybe set a timeout option on psexec?

Riv
  • 1,849
  • 1
  • 16
  • 16
  • I can't use the -d option as I need to only continue once the remote service has finished not just after it has started. I would use a timeout but the operation completed by the remote service can take between a couple of minutes up to an overnight period so it's not really a viable option. – Jack Eker Jun 28 '13 at 15:33
  • If you run psexec from command line, does it execute successfully? If so, maybe create a batch file to execute psexec and have your code start a process for the batch file. – Riv Jun 28 '13 at 16:05
  • Reasoning is that maybe psexec isn't redirecting output to your applicaation correctly – Riv Jun 28 '13 at 16:12