Reputation: 13313
I'm trying to do something like this :
var process = Process.Start("MyGame.exe");
process.WaitForExit();
MessageBox.Show("Game is closed");
The thing is when I Alt + Tab
the game my application thinks the game has exited and then displays the message. I want to completely wait for the program to be shut down before the message appears.
Is there a way around this ?
Upvotes: 1
Views: 199
Reputation: 942307
Alt+Tabbing away from a program that uses DirectX generates a "device lost" error code. Described in this MSDN page:
By design, the full set of scenarios that can cause a device to become lost is not specified. Some typical examples include loss of focus, such as when the user presses ALT+TAB or when a system dialog is initialized. Devices can also be lost due to a power management event, or when another application assumes full-screen operation. In addition, any failure from IDirect3DDevice9::Reset puts the device into a lost state.
This error can be hard to deal with in the game logic since it strikes right in the heart of the game loop. Apparently the programmer of your game found a very hacky but effective solution: he simply restarts the program. That gets the program state restored and get the game going again as soon as it re-acquires the display.
A possible workaround is to sleep for a while after WaitForExit() completes, then use Process.GetProcessesByName() to find out if it started back up. Which gets you a new Project reference, rinse and repeat.
Upvotes: 2