C# 从远程执行的.exe获取响应

C# 从远程执行的.exe获取响应,c#,.net,cmd,remote-server,psexec,C#,.net,Cmd,Remote Server,Psexec,我正在执行一个位于远程服务器上的.exe,但是PsExec似乎挂起并且本地服务不退出。.exe确实在服务器上成功运行,但完成后,我希望本地服务退出 这是我在atm机上的代码: Process p = new Process(); p.StartInfo.UseShellExecute = false; p.StartInfo.CreateNoWindow = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.Redirect

我正在执行一个位于远程服务器上的.exe,但是PsExec似乎挂起并且本地服务不退出。.exe确实在服务器上成功运行,但完成后,我希望本地服务退出

这是我在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();
processp=新流程();
p、 StartInfo.UseShellExecute=false;
p、 StartInfo.CreateNoWindow=true;
p、 StartInfo.RedirectStandardOutput=true;
p、 StartInfo.RedirectStandardError=true;
p、 StartInfo.RedirectStandardInput=true;
p、 StartInfo.FileName=@“C:\ProgramFiles(x86)\PSTools\PsExec.exe”;
p、 StartInfo.Arguments=@“\\”;
p、 Start();
//我尝试了以下方法,以在收到响应时退出本地服务
//已收到,但仍挂起。
//字符串输出=p.StandardOutput.ReadToEnd();
//string errormessage=p.StandardError.ReadToEnd();
//p、 WaitForExit();

远程服务完成后,我需要做什么才能退出本地服务?

您是否尝试了
p.WaitForInputIdle()

-------------更新


GUI与非GUI的区别:

您可以尝试使用-d选项运行psexec,这是推荐用于非交互式应用程序的选项。或者你可以在psexec上设置一个超时选项?

我没有,但我只是设置了,并且我得到了错误“WaitForInputIdle失败。这可能是因为进程没有图形界面。”远程.exe只是启动一个控制台来运行一些操作。谢谢你的建议:)当然是奇怪的错误(以前从未见过)。但看看我在google中键入时发现的结果吧,这似乎是解决您问题的一个解决方案:其中一个答案提供了跟踪进程是否在有GUI和没有GUI的情况下退出的选项。我不能使用-d选项,因为我只需要在远程服务完成后才继续,而不仅仅是在它启动后。我会使用超时,但远程服务完成的操作可能需要几分钟到一整夜的时间,因此这不是一个切实可行的选项。如果从命令行运行psexec,它是否成功执行?如果是这样,可以创建一个批处理文件来执行psexec,并让代码为该批处理文件启动一个进程。原因是psexec可能没有正确地将输出重定向到应用程序