2

I'm developing a Launcher application for games. Much like XBOX Dashboard in XNA. I want to open back my program when the process which it started(the game) exits. With a simple game this is working:

[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);
}

The Game1.Handle is got from:

Handle = Window.Handle;

In the Game1's Load Content method.

My question is how I can make the window open up after all the child process that the ran process has started is finished?

Like a launcher launches a game.

I think some more advanced programmer may know the trick.

Thanks in advance!

Daniel Sharp
  • 169
  • 1
  • 2
  • 12

1 Answers1

3

You could use Process.Exited event

 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();

More appropriate way for the game is to use named semaphore, but I would suggest you to start with Exited event, and then when you understand how it works, move to semaphore

Vitaly
  • 2,064
  • 19
  • 23
  • But how can I grab "exit events" of subprocesses of the procces which I run with my function? I only start one process and then it exits and start another one. I want to track down that another one. – Daniel Sharp Aug 09 '13 at 12:36
  • Sorry, I thought you create all child processes, not only one. – Vitaly Aug 09 '13 at 13:15
  • If you are the owner of the code of subprocesses, then you can still use semaphore – Vitaly Aug 09 '13 at 13:17
  • 1
    Try answer from [this](http://stackoverflow.com/questions/7189117/find-all-child-processes-of-my-own-net-process-find-out-if-a-given-process-is) link – Vitaly Aug 09 '13 at 13:22
  • The link you gave works like charm. So I will accept you answer. Thanks! – Daniel Sharp Aug 10 '13 at 11:12