programing

프로세스가 끝날 때까지 대기

iphone6s 2023. 5. 7. 11:15
반응형

프로세스가 끝날 때까지 대기

나는 하는 애플리케이션이 있습니다.

Process.Start()

다른 응용 프로그램 'ABC'를 시작합니다.해당 응용 프로그램이 종료될 때까지 기다렸다가(프로세스가 종료될 때까지) 실행을 계속하고 싶습니다.어떻게 해야 하나요?

애플리케이션 'ABC'의 여러 인스턴스가 동시에 실행될 수 있습니다.

당신은 그냥 이것을 원하는 것 같아요.

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

방법은 MSDN 페이지를 참조하십시오.또한 시간 초과를 지정할 수 있는 과부하가 발생하므로 잠재적으로 언제까지 기다릴 필요가 없습니다.

사용 ? 차단하지 않으려면 이벤트를 구독하시겠습니까?만약 그렇게 해도 당신이 원하는 것이 되지 않는다면, 우리에게 당신의 요구 사항에 대한 더 많은 정보를 주시기 바랍니다.

응용프로그램에서 다음 작업을 수행합니다.

Process process = new Process();
process.StartInfo.FileName = executable;
process.StartInfo.Arguments = arguments;
process.StartInfo.ErrorDialog = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
process.Start();
process.WaitForExit(1000 * 60 * 5);    // Wait up to five minutes.

유용한 기능이 몇 가지 더 있습니다.

종료 대기를 사용하거나 HasExited 속성을 캡처하고 UI를 업데이트하여 사용자에게 "알림" 상태를 유지할 수 있습니다(기대 관리).

System.Diagnostics.Process process = System.Diagnostics.Process.Start("cmd.exe");
while (!process.HasExited)
{
    //update UI
}
//done

나는 그 사건을 겪었습니다.Process.HasExited프로세스에 속한 창을 닫은 후에도 변경되지 않았습니다.그렇게Process.WaitForExit()또한 작동하지 않았습니다.제가 모니터링을 해야 했습니다.Process.Responding그렇게 창문을 닫은 후 거짓이 되었습니다.

while (!_process.HasExited && _process.Responding) {
  Thread.Sleep(100);
}
...

아마도 이것은 누군가에게 도움이 될 것입니다.

과정.Wait For Exit이 바로 당신이 찾고 있는 것일 겁니다.

Microsoft 예제 참조: [https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.enableraisingevents?view=netframework-4.8 ]

설정하는 것이 가장 좋습니다.

myProcess.EnableRaisingEvents = true;

그렇지 않으면 코드가 차단됩니다.또한 추가 속성이 필요하지 않습니다.

// Start a process and raise an event when done.
myProcess.StartInfo.FileName = fileName;
// Allows to raise event when the process is finished
myProcess.EnableRaisingEvents = true;
// Eventhandler wich fires when exited
myProcess.Exited += new EventHandler(myProcess_Exited);
// Starts the process
myProcess.Start();

// Handle Exited event and display process information.
private void myProcess_Exited(object sender, System.EventArgs e)
{
Console.WriteLine(
                  $"Exit time    : {myProcess.ExitTime}\n" +
                  $"Exit code    : {myProcess.ExitCode}\n" +
                  $"Elapsed time : {elapsedTime}");
}

Jon Skeet이 말한 것처럼, 다음과 같이 사용합니다.Process.Exited:

proc.StartInfo.FileName = exportPath + @"\" + fileExe;
proc.Exited += new EventHandler(myProcess_Exited);
proc.Start();
inProcess = true;

while (inProcess)
{
    proc.Refresh();
    System.Threading.Thread.Sleep(10);
    if (proc.HasExited)
    {
        inProcess = false;
    }
}

private void myProcess_Exited(object sender, System.EventArgs e)
{
    inProcess = false;
    Console.WriteLine("Exit time:    {0}\r\n" +
      "Exit code:    {1}\r\n", proc.ExitTime, proc.ExitCode);
}

사용해 보십시오.

string command = "...";
var process = Process.Start(command);
process.WaitForExit();

언급URL : https://stackoverflow.com/questions/3147911/wait-until-a-process-ends

반응형