Single instance application

G

Guest

I am programming in c# and making a windows forms application.
I don't want to make a windows service.
How can I make sure that only once instance of the application runs ?
If you try to run another instance , it should bring the running instance to
the foreground and exit itself.

The second thing that I want to achieve is as follows :
This UI will be scheduled to run at system startup. when I login , I can see
the
process running but with no UI, meaning its running in the background.
Now I try running another instance, this should bring the first instance to
the foreground.

Is it actually possible to bring the instance which does not show a UI to
bring to the foreground and show the window ?
I have tried ShowWindow and SetForeGroundWindow but with no success.

I really appreciate any help here.

Thanks,
Alok
 
G

Guest

Alok,

If you really want the application to start at system startup, before you
log on, then I think a windows service is the way to go. Why don't you want
to do that? You could create a UI that communicates with the service (via
remoting) and start that UI when you log on.

Joris
 
S

Sericinus hunter

Alok said:
I am programming in c# and making a windows forms application.
I don't want to make a windows service.
How can I make sure that only once instance of the application runs ?
If you try to run another instance , it should bring the running instance to
the foreground and exit itself.

Try to play around with this code:

public static void Main()
{
Process pr = Process.GetCurrentProcess();
string appName = pr.ProcessName;
Process[] runningProcesses = Process.GetProcessesByName(appName);

// at this point we either have one or two processes in the array;
// if one, then we just started, if two, this is the second instance
// and we need to activate the first one and exit

if(runningProcesses.Length == 1)
Application.Run(new MainWindow());
else
{
IntPtr hwnd = IntPtr.Zero;

if(!runningProcesses[0].MainWindowHandle.Equals(hwnd))
handle = runningProcesses[0].MainWindowHandle;
else if(!runningProcesses[1].MainWindowHandle.Equals(hwnd))
handle = runningProcesses[1].MainWindowHandle;

Win32.ShowWindowAsync(hwnd, 2); // SW_MINIMIZE
Win32.ShowWindowAsync(hwnd, 9); // SW_RESTORE
}
}
 
S

Stoitcho Goutsev \(100\)

Alok,

Usually in order to find out if an instance is already running programmer
use mutexes. This question has been asked a lot. Try to google for an
answer; you'll find sulution and code snippets how to do that.
 
G

Guest

Alok said:
I am programming in c# and making a windows forms application.
I don't want to make a windows service.
How can I make sure that only once instance of the application runs ?
If you try to run another instance , it should bring the running instance to
the foreground and exit itself.

Hi Alok,

We offer a .NET component FREE for non-commercial use which will do this for
you:

http://www.mini-tools.com/goto/comm
The second thing that I want to achieve is as follows :
This UI will be scheduled to run at system startup. when I login , I can see
process running but with no UI, meaning its running in the background.
Now I try running another instance, this should bring the first instance to
the foreground.

Is it actually possible to bring the instance which does not show a UI to
bring to the foreground and show the window ?
I have tried ShowWindow and SetForeGroundWindow but with no success.

Yes. Try this:

SetForegroundWindow( hWnd );
SetActiveWindow( hWnd );

You will need these definitions:

using System.Runtime.InteropServices;
[DllImport("user32.dll")]
static private extern bool SetForegroundWindow( IntPtr hWnd );
[DllImport("user32.dll")]
static private extern IntPtr SetActiveWindow(IntPtr hWnd);
 

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