How to Run App only one instance

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I want to run my app only one instance, i.e. only one session is allowed to display

Thank
 
'--------------------------
Function PrevInstance() As Boolean
If
UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrent
Process.ProcessName)) > 0 Then
MsgBox("Application is still running", MsgBoxStyle.Information)
Return True
Else
Return False
End If
End Function

Sub Main()
If PrevInstance() Then Exit Sub

Application.Run(...)
End Sub
'-----------------------

Hope this helps,
Claudio
 
Thanks Claudio
Your codes seem work fine. One more question, how to focus the previous app when it's called one more time
 
* "Claudio Di Flumeri said:
UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrent
Process.ProcessName)) > 0 Then
MsgBox("Application is still running", MsgBoxStyle.Information)
Return True

Notice that the process name isn't always unique, even if the process
has a very simple name like "Editor".
 
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
}
}
 
Sorry, I forgot that this is a VB newsgroup. I will convert it to VB and
post later.
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
}
}


Ken Tucker said:
Hi,

You need to use a mutex
http://groups.google.com/groups?selm=#[email protected]
 
Okay, here's the vb form of that. Again, sorry for the C# intrusion:

'We're going to add the current username to this base string so that
multiple user can run it
Private Const PREVIOUS_INSTANCE_MUTEXBASE As String =
"MyApp_B8533159_7548_4dda_91C4_98FAF25A361E-"

Public Shared Sub Main()
Dim currentUsername As String = ""
Try
'Mutex names apparently cannot have the \ character in them.
currentUsername =
System.Security.Principal.WindowsIdentity.GetCurrent().Name.Replace("\\",
"_")
Catch ex As System.Security.SecurityException
Dim msg As String = "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)
Exit Sub
End Try

Dim m As New System.Threading.Mutex(True,
PREVIOUS_INSTANCE_MUTEXBASE + currentUsername)
Dim ableToLock As Boolean = (m.WaitOne(1, True))
If (ableToLock) Then
Application.Run()
m.ReleaseMutex()
Else
'// Previous instance found
'// TODO: Find running process and Activate() the current dialog
if applicable()
End If
End Sub

Chris Becker said:
Sorry, I forgot that this is a VB newsgroup. I will convert it to VB and
post later.
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
}
}


Ken Tucker said:
Hi,

You need to use a mutex
http://groups.google.com/groups?selm=#[email protected]
 
Back
Top