How can I get Process.MainWindowHandle when the window is hidden

I have a WPF application. When it starts, I need to check if the instance is running. If so, then I will β€œshow” the executable instance using PInvoke:

SendNotifyMessage(proc.MainWindowHandle, ShowYourself, IntPtr.Zero, IntPtr.Zero); 

where proc is another instance executing

the problem is that proc hiding in the system tray. MainWindowHandle is 0. I did a lot of searching, but no luck getting MainWindowHandle .

Does anyone know how?

thanks

+4
source share
1 answer

The best way to use a cross-process handler, such as a mutex. Here is a good example. One more thing: if you want to open a hidden application from another instance, you must make a connection between the client and the server between the two applications. An application that is already running must be a server and must listen to make the command visible. It is quite simple, but it takes a long time to understand.

UPDATED: Here I installed a simple library that allows you to manage application instances. It is very simple, but I leave comments

ApplicationSingleInstance is the main class for managing application instances. When an application starts checking the IsInstanceAlreadyRun property, if it calls the StartServer () method, if false calls the NotifyAboutNewInstance () method. You must also subscribe to the NewApplicationInstanceLoaded event to find out if a new instance of the application is running. Remember to unsubscribe from events and delete an instance of ApplicationSingleInstance

UPDATED: I posted this lib and explanation on codeproject

0
source

Source: https://habr.com/ru/post/1340260/


All Articles