2013-08-09 5 views
1

저는 게임용 실행기 응용 프로그램을 개발 중입니다. XNA의 XBOX Dashboard와 매우 비슷합니다. 프로그램을 시작한 (게임) 프로세스가 종료되면 프로그램을 다시 열고 싶습니다. 간단한 게임이 작동됩니다 :실행 프로세스의 모든 하위 프로세스가 완료 될 때까지 기다리십시오. C#

[DllImport("user32.dll")] 
[return: MarshalAs(UnmanagedType.Bool)] 
static extern bool SetForegroundWindow(IntPtr hWnd); 

[DllImportAttribute("User32.DLL")] 
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); 
private const int SW_SHOW = 5; 
private const int SW_MINIMIZE = 6; 
private const int SW_RESTORE = 9; 

public void Run(file) 
{ 
    ProcessStartInfo startInfo = new ProcessStartInfo(file); 
    Environment.CurrentDirectory = Path.GetDirectoryName(file); 
    startInfo.Verb = "runas"; 
    var process = Process.Start(startInfo); 
    process.WaitForExit(); 
    ShowWindow(Game1.Handle, SW_RESTORE); 
    SetForegroundWindow(Game1.Handle); 
} 

Game1.Handle부터 받고 있습니다 :

Handle = Window.Handle; 

을 GAME1의로드 컨텐츠 방법.

제 질문은 어떻게 실행 프로세스가 시작된 모든 하위 프로세스가 끝난 후 창을 열 수 있습니까?

런처처럼 게임을 실행합니다.

좀 더 고급 프로그래머가 트릭을 알고 있다고 생각합니다.

미리 감사드립니다.

답변

2

당신은 게임에 대한

int counter == 0; 
    ..... 

    //start process, assume this code will be called several times 
    counter++; 
    var process = new Process(); 
    process.StartInfo = new ProcessStartInfo(file); 

    //Here are 2 lines that you need 
    process.EnableRaisingEvents = true; 
    //Just used LINQ for short, usually would use method as event handler 
    process.Exited += (s, a) => 
    { 
     counter--; 
     if (counter == 0)//All processed has exited 
     { 
     ShowWindow(Game1.Handle, SW_RESTORE); 
     SetForegroundWindow(Game1.Handle); 
     } 
    } 
    process.Start(); 

더 적절한 방법이 semaphore 이름 사용하는 것입니다 Process.Exited 이벤트를 사용할 수 있습니다,하지만 난 당신이 어떻게 작동하는지 이해 할 때 종료 한 후 이벤트로 시작하고 제안, 세마포어로 이동

+0

그러나 기능을 사용하여 실행되는 프로세스의 하위 프로세스에서 "종료 이벤트"를 어떻게 잡아낼 수 있습니까? 나는 단지 하나의 프로세스를 시작한 다음 종료하고 다른 프로세스를 시작합니다. 나는 다른 것을 추적하고 싶다. –

+0

죄송합니다. 나는 하나뿐 아니라 모든 하위 프로세스를 생성한다고 생각했습니다. – Vitaly

+0

하위 프로세스 코드의 소유자 인 경우 세마포를 계속 사용할 수 있습니다. – Vitaly