public static void BringProcessWindowToFront(Process process) {
if (process == null) return;
var handle = process.MainWindowHandle;
for (var i = 0; !IsWindowInForeground(handle); i++) {
if (i == 0) {
Thread.Sleep(150);
}
if (IsIconic(handle)) {
ShowWindow(handle, WindowShowStyle.Restore);
} else {
SetForegroundWindow(handle);
}
Thread.Sleep(250);
if (IsWindowInForeground(handle)) {
Thread.Sleep(500);
return;
}
if (i > 120) throw new Exception("Could not set process window to the foreground");
}
}