2014-04-30 2 views
2

C#을 사용하여 실행해야하는 배치 파일이 여러 개 있습니다. 문제는 데몬 프로세스를 실행중인 첫 번째 배치 파일의 끝에 있습니다. 이 때문에 WaitForExit() 때문에 두 번째 배치 파일이 실행되지 않습니다. 나는 (이 데몬을 실행에 도달 할 때까지) 첫번째 배치 파일을 실행하는 데 필요한 시간이 얼마나 잘 모릅니다 때문에 내가에서 어쩌구 저쩌구을 가지고 있고, BatchFile1.bat 내부C# 실행중인 데몬 명령 줄

BatchFile1.bat -> 
BatchFile2.bat -> 
BatchFile3.bat -> 
BatchFile4.bat 

이을받을 수 없어 그것은 당연히 cmd.exe를 실행하기 위해

//not exit , even if there is error 
public void Run_Process(string process_name, string s)  
{     
    Process myProcess = new Process(); 

    myProcess.StartInfo.UseShellExecute = false; 
    myProcess.StartInfo.FileName = "cmd.exe"; 
    myProcess.StartInfo.Arguments = "/C " + process_name + s; 

    myProcess.StartInfo.CreateNoWindow = true; 
    myProcess.StartInfo.RedirectStandardOutput = true; 
    myProcess.StartInfo.RedirectStandardError = true; 
    myProcess.Start(); 
    string standard_output = myProcess.StandardOutput.ReadToEnd(); 
    myProcess.WaitForExit(); 
    Console.WriteLine(standard_output); 
} 
+1

당신은 백그라운드에서 데몬을 시작 박쥐 파일을 변경할 수 ? – Blorgbeard

+0

박쥐 파일을 변경할 수 없습니다. 금지됨 –

+0

.bat 코드를 게시 할 수 있습니까? – Blorgbeard

답변

0

를 종료하지 않습니다 데몬 프로세스를 실행 끝이 작동 될 수 있습니다

static void ExecuteCommand(string command) 
{ 
int exitCode; 
ProcessStartInfo processInfo; 
Process process; 

processInfo = new ProcessStartInfo("cmd.exe", "/c " + command); 
processInfo.CreateNoWindow = true; 
processInfo.UseShellExecute = false; 
// *** Redirect the output *** 
processInfo.RedirectStandardError = true; 
processInfo.RedirectStandardOutput = true; 

process = Process.Start(processInfo); 
process.WaitForExit(); 

// *** Read the streams *** 
string output = process.StandardOutput.ReadToEnd(); 
string error = process.StandardError.ReadToEnd(); 

exitCode = process.ExitCode; 

Console.WriteLine("output>>" + (String.IsNullOrEmpty(output) ? "(none)" : output)); 
Console.WriteLine("error>>" + (String.IsNullOrEmpty(error) ? "(none)" : error)); 
Console.WriteLine("ExitCode: " + exitCode.ToString(), "ExecuteCommand"); 
process.Close(); 
} 

static void Main() 
{ 
ExecuteCommand("echo testing"); 
}