Here is some code I use [C#]. Note that this allows only one instance PER
USER. So on WinXP and above, each USER is only allowed to run one instance
of the app.
Any comments or corrections are greatly appreciated.
// We're going to add the current username to this base string so that
multiple user can run it
private const string PREVIOUS_INSTANCE_MUTEXBASE =
"MyApp_B8533159_7548_4dda_91C4_98FAF25A361E-";
/// <summary>The main entry point for the application.</summary>
public static void Main()
{
string currentUsername = "";
try
{
currentUsername =
System.Security.Principal.WindowsIdentity.GetCurre nt().Name.Replace("\\","_"
); //Mutex names apparently cannot have the \ character in them.
}
catch (System.Security.SecurityException)
{
string msg = "You appear to be running the EXE from a network share.\n";
msg += "In this case the application is running in \"restricted security\"
mode and remoting is disabled.\n";
msg += "To fix this, copy the exe to your local PC or grant
ControlPrincipal rights to the mapped drive [using the .NET MMC Snapin].";
MessageBox.Show(msg, "MyApp", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Mutex m = new Mutex(true, PREVIOUS_INSTANCE_MUTEXBASE + currentUsername);
bool ableToLock = (m.WaitOne(1, true));
if (ableToLock)
{
if (GetPasswordFile())
{
TrayIcon.Show();
Application.Run();
}
m.ReleaseMutex();
}
else
{
// Previous instance found
// TODO: Find running process and Activate() the current dialog if
applicable
}
}
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:em**************@TK2MSFTNGP11.phx.gbl...
Hi,
You need to use a mutex
http://groups.google.com/groups?selm...TNGP09.phx.gbl
Ken
-------------------
"Li Pang" <an*******@discussions.microsoft.com> wrote in message
news:E8**********************************@microsof t.com... Hi,
I want to run my app only one instance, i.e. only one session is allowed
to display.
Thanks