On Fri, 30 May 2008 06:10:49 -0700, <aa*******@gmail.comwrote:
I have a windows form application, where the requirement is as
follows...
If the application is already running, and the user tries to open
another instance of the application, the already running instance of
the application should be activated.
Also my application can be in the system tray (and not be present in
the taskbar), so even in this condition the application should be
activated (i.e. application's main screen should be displayed)
As Ignacio says, you can use a named Mutex to detect the case. As for how
to actually perform the activation, AFAIK there's nothing in .NET that
allows you to do this. You'll need to use p/invoke with the
SetForegroundWindow() function, and of course that means you'll have to
actually find the correct window, also via p/invoke (again, AFAIK there's
nothing in .NET to allow you to enumerate top-level windows from other
processes).
There are restrictions on whether your own process will be allowed to
bring the other window to the foreground. When it can't, the window entry
in the task bar will flash instead, which is correct behavior. However,
most likely the new instance of the application will be the foreground
task and will have the rights to bring the other application to the
foreground, so I think that normally this will do exactly what you want.
Pete