G
Guest
Hi
I want to run my app only one instance, i.e. only one session is allowed to display
Thank
I want to run my app only one instance, i.e. only one session is allowed to display
Thank
* "Ken Tucker said:
* "Claudio Di Flumeri said:UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrent
Process.ProcessName)) > 0 Then
MsgBox("Application is still running", MsgBoxStyle.Information)
Return True
Chris Becker said: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.GetCurrent().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
}
}
http://groups.google.com/groups?selm=#[email protected]Ken Tucker said:Hi,
You need to use a mutex
Chris Becker said:Sorry, I forgot that this is a VB newsgroup. I will convert it to VB and
post later.
System.Security.Principal.WindowsIdentity.GetCurrent().Name.Replace("\\","_"Chris Becker said: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 =http://groups.google.com/groups?selm=#[email protected]); //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 said:Hi,
You need to use a mutex