开发者

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

开发者 https://www.devze.com 2023-02-11 00:34 出处:网络
I have a WPF application. When it starts up, I need to check if there is already an instance running. If that is the case, then I \"show\" the running instance by using PInvoke:

I have a WPF application. When it starts up, I need to check if there is already an instance running. If that is the case, then I "show" the running instance by using PInvoke:

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

where proc is the other instance that's running

the problem is, when the proc is hidden in system tray. MainWindowHandle is 0. I did a lot of search but no luck to get the MainWindowH开发者_运维百科andle.

Anybody knows how?

Thanks


The best way it's to use cross-process handler, like a mutex. Here is a good example. One more thing, if you want to open hidden application from another instance, you should make client-server communication between two applications. Application that is already running should be server and should listen for command to become visible. Thats rather simple but it takes a lot of time for understanding.

UPDATED: Here I've committed a simple library that allows you control application instances. It's very simple but i leave some comments

ApplicationSingleInstance - it's the main class for controlling application instances When application starts check property IsInstanceAlreadyRun, if it's true invoke StartServer() method, if false invoke NotifyAboutNewInstance() method. Also you should subscribe on NewApplicationInstanceLoaded event to know if new application instance has been started. Do not forget to unsubscribe from events and dispose instance of ApplicationSingleInstance

UPDATED: I've posted this lib and explanation on codeproject

0

精彩评论

暂无评论...
验证码 换一张
取 消