How to open the already running application

A

aagarwal8

Hi,

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)

Any help in this regard is appreciated.


Regards,
Ankit!
 
I

Ignacio Machin ( .NET/ C# MVP )

Hi,

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)

Any help in this regard is appreciated.

Regards,
Ankit!

Hi,

You have to use something to detect that the app is running already,
take a look at Mutex
To activate the prev. instance you have to get the hWnd of it and make
it the foremost window. Look in the archive of this NG as this is a
recurrent question
 
M

mpetrotta

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

If it's sufficient to activate the application's main window, rather
than whatever's currently at the top of the z-order, you can use
Process.MainWindowHandle:

foreach (Process p in Process.GetProcessesByName("myapp"))
{
SetForegroundWindow(p.MainWindowHandle);
}
....
[DllImport("User32.dll")]
static extern bool SetForegroundWindow(IntPtr hwnd);

Watch out for minimized windows, which will stay minimized after
activation.

Another approach I've used before is interprocess communication to
send messages to the currently-running app. This can get a little
involved, though.

Michael
 
A

aagarwal8

thanks for the inputs Michael...

However, i was looking forward to an IPC solution. Any leads in that
regard?

Ankit!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top