I have a wpf application on .net 4.8. If i close the app i use MainWindow.Hide() to bring it in Systemtray and to minimize the app. But if a user starts a new instance of the app it should be bring to foreground the minimized app. For this I use this pice of code:
var currentProcess = System.Diagnostics.Process.GetCurrentProcess();
var runningProcesses = Process.GetProcesses().FirstOrDefault(p => p.ProcessName == currentProcess.ProcessName && p.Id != currentProcess.Id);
if (runningProcesses != null)
{
var window = runningProcesses.MainWindowHandle;
ShowWindowAsync(window, 1); //SW_SHOWNORMAL
SetForegroundWindow(window);
this.Close();
}
If the app is only minimized it works perfekt. If it is hided (Systemtray) the MainWindowHandle is always 0. I have noticed that if I hide the app it goes from apps section to the background process section in the taskmanager. Is there a way to avoid this action or is there a way to bring it in foreground? Thanks