當我在調試模式下運行代碼時,測試完成,當我在運行測試中運行它時,ps exec似乎掛起並永遠不會返回我將kill但也沒有完成。PSExec在運行測試時掛起,但在調試會話中工作正常
爲什麼在調試時完成此操作,而不是在運行模式下完成。
這裏有什麼愚蠢的東西我失蹤了嗎?
public class RemoteExeInvokerTask :IQaTask
{
public string TargetHostName { get; set; }
public string TargetHostUserName { get; set; }
public string TargetPassword { get; set; }
public string TargetExecutableWithParams { get; set; }
public string DoWork()
{
string psExecUtility = Path.GetDirectoryName(Assembly
.GetExecutingAssembly()
.Location)
+ "\\PsTools\\psExec.exe";
string quotedArgs = string.Format("\\\\{0} -u {1} -p {2} {3} ",
TargetHostName,
TargetHostUserName,
TargetPassword,
TargetExecutableWithParams);
ProcessStartInfo processInfo = new ProcessStartInfo(psExecUtility, quotedArgs);
processInfo.RedirectStandardError = true;
processInfo.RedirectStandardOutput = true;
processInfo.UseShellExecute = false;
processInfo.CreateNoWindow = true;
Process process = Process.Start(processInfo);
process.WaitForExit();
string error = process.StandardError.ReadToEnd();
string output = process.StandardOutput.ReadToEnd();
int exitCode = process.ExitCode;
process.Close();
if (Process.GetProcessById(process.Id) != null)
{
process.Kill();
}
if (exitCode <= -1)
{
string errorMessage = string.Format("Process Exited with ErrorMessge {0} {1} "
+ "OutputMessage {2}",
error,
Environment.NewLine,
output);
throw new MyApplicationException(errorMessage);
}
return output;
}
測試代碼。
[Test]
[ExpectedException(typeof(MyApplicationException))]
[Description("Expect a exception message form the Server.")]
public void RemoteInvokeOfJobWrapperFromCommandLine()
{
RemoteExeInvokerTask task = new RemoteExeInvokerTask {
TargetHostName = "Serverd02",
TargetHostUserName = @"corp\username",
TargetPassword = @"password",
TargetExecutableWithParams = @" E:\D01\Home.Framework.JobExecutionEngine\Home.Framework.JobexecutionEngine.exe 99999 1"};
string value = task.DoWork();
Assert.IsNotNull(value);
}
如果將字符串quotedArgs = string.Format(「\\\\ {0} -u {1} -p {2} {3}」改爲string quotedArgs = string.Format(@「{0} - ({{0} -u {1} -p {2} {3}「@ TargetHostName,@ TargetHostUserName,@ TargetPassword,TargetExecutableWithParams4 – MethodMan 2012-01-04 19:35:11
我建議使用'Path.Combine(String,String)',專門用於'string psExecUtility ='代碼。 – 2012-01-04 19:44:16
問題是,如果我們註釋掉這些位並忽略StanderdError和StandardOutput,processInfo.UseShellExecute = false好吧,只有當我們想讀它時,它纔會死亡,說它的工作非常複雜,所以這可能是Win7 64位shell的一個問題, – bhushanvinay 2012-01-05 16:17:18