5

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 MainWindowHandle.

Anybody knows how?

Thanks

sean717
  • 11,759
  • 20
  • 66
  • 90
  • 2
    possible duplicate of [What is the correct way to create a single instance application?](http://stackoverflow.com/questions/19147/what-is-the-correct-way-to-create-a-single-instance-application) – Hans Passant Feb 18 '11 at 03:26

1 Answers1

0

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

Disposer
  • 667
  • 1
  • 7
  • 23