2012-01-04 104 views
0

當我在調試模式下運行代碼時,測試完成,當我在運行測試中運行它時,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); 
} 
+0

如果將字符串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

+0

我建議使用'Path.Combine(String,String)',專門用於'string psExecUtility ='代碼。 – 2012-01-04 19:44:16

+0

問題是,如果我們註釋掉這些位並忽略StanderdError和StandardOutput,processInfo.UseShellExecute = false好吧,只有當我們想讀它時,它纔會死亡,說它的工作非常複雜,所以這可能是Win7 64位shell的一個問題, – bhushanvinay 2012-01-05 16:17:18

回答

0

基本上替換,你必須 「\」 與@雙斜線在變量前面例如

what if you change string 
quotedArgs = string.Format("\\\\{0} -u {1} -p {2} {3} " 

讀取這樣
串quotedArgs =的String.Format(@「{0 } -u {1} -p {2} {3}「 或

string quotedArgs = string.Format("{0} -u {1} -p {2}{3} ", 
@TargetHostName, 
@TargetHostUserName, 
@TargetPassword, 
@TargetExecutableWithParams4 

也替換 串psExecUtility = Path.GetDirecto ryName(Assembly.GetExecutingAssembly()。Location)+「\ PsTools \ psExec.exe」;

到這樣的事情

string psExecUtility = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "@PathandExName 

//這應該是一個變種這個 「\ PsTools \ psExec.exe」;

+0

這沒有奏效。試過所有的組合,但dosnt工作。當我改變它爲string.Format(「{0} -u {1} -p {2} {3}」,我得到一個錯誤,該參數是無效的。改變我做了更改做一個路徑。結合也沒有什麼似乎工作。 – bhushanvinay 2012-01-05 11:54:38

相關問題