2010-06-19 7 views
4

Process.Start를 호출하지만 현재 스레드를 차단합니다.Process.Start가 차단 중입니다.

pInfo = new ProcessStartInfo("C:\\Windows\\notepad.exe"); 

// Start process 
mProcess = new Process(); 
mProcess.StartInfo = pInfo; 
if (mProcess.Start() == false) { 
    Trace.TraceError("Unable to run process {0}."); 
} 

프로세스가 닫히더라도 코드는 더 이상 응답하지 않습니다.

하지만 Process.Start가 실제로 차단되어 있어야합니까? 무슨 일이야?


using System; 
using System.Diagnostics; 
using System.Threading; 
using System.Windows.Forms; 

namespace Test 
{ 
    class Test 
    { 
     [STAThread] 
     public static void Main() 
     { 
      Thread ServerThread = new Thread(AccepterThread); 
      ServerThread.Start(); 

      Console.WriteLine (" --- Press ENTER to stop service ---"); 
      while (Console.Read() < 0) { Application.DoEvents(); } 

      Console.WriteLine("Done."); 
     } 

     public static void AccepterThread(object data) 
     { 
      bool accepted = false; 

      while (true) { 
       if (accepted == false) { 
        Thread hThread = new Thread(HandlerThread); 
        accepted = true; 
        hThread.Start(); 
       } else 
        Thread.Sleep(100); 
      } 
     } 

     public static void HandlerThread(object data) 
     { 
      ProcessStartInfo pInfo = new ProcessStartInfo("C:\\Windows\\notepad.exe"); 

      Console.WriteLine("Starting process."); 

      // Start process 
      Process mProcess = new Process(); 
      mProcess.StartInfo = pInfo; 
      if (mProcess.Start() == false) { 
       Console.WriteLine("Unable to run process."); 
      } 
      Console.WriteLine("Still living..."); 
     } 
    } 
} 

콘솔 출력은 (이 과정은 제대로 시작) :

--- 보도 과정을 시작 서비스 --- 을 중지갑니다.

찾았 :

는 [STAThread]

는 Process.Start를 차단을 만든다. STAThread and Multithreading을 읽었지만 개념을 Process.Start 동작과 연결할 수 없습니다.

AFAIK, STAThread는 이 필요합니다. by Windows.Form. Windows.Form을 사용할 때이 문제를 해결하는 방법은 무엇입니까? 뉴스 지옥에 대한


:

내 응용 프로그램을 다시 경우, 내가 제대로 응용 프로그램 작업을 실행하지만, 처음 시간 나는 디버깅을 중지하고, 다시 문제 araise을 IY를 다시 시작합니다.

응용 프로그램이 디버거없이 실행될 때 문제가 발생하지 않습니다.

+0

'UseShellExecute' 및'ErrorDialog'를 true로 설정하고 생성되는 오류가 있는지 확인하십시오. – AMissico

+0

"cDaemon"클래스가하는 일을 우리가 모른다는 사실을 알기는 어렵습니다. 문제를 보여주는 짧지 만 완전한 * 프로그램을 제안하십시오. –

+0

(Console.Read와 결합 된 Application.DoEvents의 사용은 꽤 의심스러워 보입니다.) –

답변

10

아니요, Process.Start은 하위 프로세스가 완료 될 때까지 기다리지 않습니다. 그렇지 않으면 리디렉션 된 I/O와 같은 기능을 사용할 수 없습니다.

샘플 콘솔 응용 프로그램 : 이것은 내 상자에 아무런 문제 ", 난 아직 실행 해요 페이지의"인쇄

using System; 
using System.Diagnostics; 

public class Test 
{ 
    static void Main() 
    { 
     Process p = new Process { 
      StartInfo = new ProcessStartInfo("C:\\Windows\\notepad.exe") 
     }; 
     p.Start(); 
     Console.WriteLine("See, I'm still running"); 
    } 
} 

- 그것은 당신의 상자에 무엇을하고 있는지?

+0

@usr : 업데이트합니다. –

+0

Process Start가 메인 스레드를 차단합니다. –

+0

@CurtisWhite : 정말 정상적으로 처리되지 않습니다. 짧지 만 완전한 예제로 새로운 질문을하는 것이 좋습니다. –

6

ProcessStartInfo을 만들고 UseShellExecute를 false로 설정합니다 (기본값은 true입니다). 코드는 다음과 같아야합니다.

pInfo = new ProcessStartInfo("C:\\Windows\\notepad.exe"); 
pInfo.UseShellExecute = false; 

// Start process 
mProcess = new Process(); 
mProcess.StartInfo = pInfo; 
if (mProcess.Start() == false) { 
    Trace.TraceError("Unable to run process {0}."); 
} 

동일한 문제가있어서 실행 파일을 직접 실행하여 실행 파일을 직접 작성하면 문제가 해결됩니다.

+0

+1 - 내가 열려고했던 Excel 통합 문서가 스레드를 차단하고 있었지만.특정 통합 문서를 열려면 ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "EXCEL.EXE"; startInfo.Arguments = filePath – Adam

+0

며칠 동안이 문제를 해결하기 위해 노력해 왔습니다. 감사합니다. –

0

WinForms 앱의 원래 포스터와 동일한 차단 동작이 발생 했으므로이 동작 테스트를 단순화하기 위해 아래 콘솔 앱을 만들었습니다.

Jon Skeet의 예제에서는 메모장을 사용합니다. 메모장은 정상적으로로드되는 데 수 밀리 초 밖에 걸리지 않으므로 스레드 블록이 눈에 띄지 않을 수 있습니다. 나는 보통 더 오랜 시간이 걸리는 Excel을 시작하려했다. StartWithPathStartWithInfo 모두

using System; 
using System.Diagnostics; 
using static System.Console; 
using System.Threading; 

class Program { 

    static void Main(string[] args) { 

     WriteLine("About to start process..."); 

     //Toggle which method is commented out: 

     //StartWithPath(); //Blocking 
     //StartWithInfo(); //Blocking 
     StartInNewThread(); //Not blocking 

     WriteLine("Process started!"); 
     Read(); 
    } 

    static void StartWithPath() { 
     Process.Start(TestPath); 
    } 

    static void StartWithInfo() { 
     var p = new Process { StartInfo = new ProcessStartInfo(TestPath) }; 
     p.Start(); 
    } 

    static void StartInNewThread() { 
     var t = new Thread(() => StartWithPath()); 
     t.Start(); 
    } 

    static string TestPath = 
     Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + 
     "\\test.xlsx"; 
} 

통화는 콘솔 응용 프로그램 내 스레드를 차단합니다. Excel 시작 화면이 닫히고 기본 창이 열릴 때까지 내 콘솔에 "프로세스 시작"메시지가 표시되지 않습니다. 엑셀의 시작 화면이 열려있는 동안

enter image description here

StartInNewThread 즉시 콘솔에서 모두 메시지를 표시합니다.

+0

아야 ... 그 문제에 대해 아직도 불안합니다. – Luca

0

다른 도메인 (우리는 이중 트러스트 된 도메인이 있음)의 네트워크 드라이브에있는 .bat 스크립트를 시작할 때이 문제가있었습니다. 원격 C# 디버거를 실행하고 Process.Start()가 무기한으로 차단하고 있는지 확인합니다.

대화 형 파워 쉘에서이 작업을 반복

, 보안 대화 상자가 진열되었습니다

enter image description here

을 지금까지 솔루션으로, this was the direction을 우리는 갔다. 작업을 수행 한 사람이 트러스트를 수행하기 위해 도메인 GPO를 수정했습니다.

관련 문제