[英]Wait for all child processes of a ran process to finish C#
我正在開發游戲的啟動器應用程序。 類似於XNA中的XBOX儀表板。 我想在開始的程序(游戲)退出時重新打開程序。 通過一個簡單的游戲,它可以正常工作:
[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的“加載內容”方法中。
我的問題是,在運行過程開始的所有子過程完成后,如何使窗口打開?
就像發射器啟動游戲一樣。
我認為一些更高級的程序員可能知道訣竅。
提前致謝!
您可以使用Process.Exited事件
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();
對於游戲,更合適的方法是使用命名信號量 ,但我建議您從Exited事件開始,然后在了解其工作原理后轉到信號量
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.