I have a WPF application. Inside the application during a button click I create a new process to open a new application like below code from my view model.
var process = Process.Start(binaryLocation
+ @"\TestApp.exe", powDBop + " " + mDBop);
processID = process.Id;
It works fine. Now the problem is if user clicks on the button again, I check whether the process is alive, and if it is alive I need to bring the Window of that application to the front .
For that I tried below code. But it is failing.
var winHandle = Process.GetProcessById(process.Id).MainWindowHandle;
HwndSource hwndSource = HwndSource.FromHwnd(winHandle);
var window = hwndSource.RootVisual as Window;
window.BringIntoView();
window.Activate();
hwndSource is not being set and remains null.
I even tried with below code and still window returns null.
var winHandle = Process.GetProcessById(process.Id).MainWindowHandle;
Window window = (Window) HwndSource.FromHwnd(winHandle).RootVisual;
Anyone?
I think you can do it with the following code
var process = Process.Start(binaryLocation
+ @"\TestApp.exe", powDBop + " " + mDBop);
SetForegroundWindow(process.MainWindowHandle);
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.