473,324 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

How to Run App only one instance

Hi

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

Thank
Nov 20 '05 #1
8 1644
'--------------------------
Function PrevInstance() As Boolean
If
UBound(Diagnostics.Process.GetProcessesByName(Diag nostics.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
Nov 20 '05 #2
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

Nov 20 '05 #3
Thanks Claudio
Your codes seem work fine. One more question, how to focus the previous app when it's called one more time
Nov 20 '05 #4
* "Claudio Di Flumeri" <cl***********@mtgc.net> scripsit:
UBound(Diagnostics.Process.GetProcessesByName(Diag nostics.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".

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #5
* "Ken Tucker [MVP]" <vb***@bellsouth.net> scripsit:
You need to use a mutex

http://groups.google.com/groups?selm...TNGP09.phx.gbl


I preferred this solution too, but some weeks ago somebody told me that
it didn't work on their machine. Don't know the reason... ;-(.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #6
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


Nov 20 '05 #7
Sorry, I forgot that this is a VB newsgroup. I will convert it to VB and
post later.
"Chris Becker" <sl*****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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



Nov 20 '05 #8
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.GetCurre nt().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" <sl*****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Sorry, I forgot that this is a VB newsgroup. I will convert it to VB and
post later.
"Chris Becker" <sl*****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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
>
>



Nov 20 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Robert Ferrell | last post by:
I have a question about assigning __call__ to an instance to make that instance callable. I know there has been quite a bit of discussion about this, and I've read all I can find, but I'm still...
4
by: | last post by:
Hi I have a list containing several instance address, for example: I'd like to invoke a method on each of these instance but I don't know : 1. if its possible 2. how to proceed
18
by: John M. Gabriele | last post by:
I've done some C++ and Java in the past, and have recently learned a fair amount of Python. One thing I still really don't get though is the difference between class methods and instance methods. I...
2
by: Mike | last post by:
Greetings, Having a major problem here. running version 8.2 on win2003 server. The problem I am having is backing up a database seems to get to the last part of the backup and then fails. This...
6
by: Hemant Shah | last post by:
Folks, I need to move HOME directory of an instance to another directory. What is the best way of doing it? Is changing password file enough? or dies DB2 store this info in it's own config? ...
6
by: Dmitry Karneyev | last post by:
Hi! I guess this question have been asked a lot of times, but please be tolerant and if you have any ideas share it. The question is: how to make availibale only one instance of application and...
2
by: Mesan | last post by:
Hello everyone, Thanks to many useful posts in this newsgroup and others, I've been able to come very close to something I've been wanting to do for a very long time. I've figured out how to...
1
by: vijay.db | last post by:
Hi Team, Very serious problem with my DB2 V8.1 Fixpack 6 running in AIX 5.1 machine. Every one hour my DB2 instance processes are killed and it's going down. Several trap files are generated in...
12
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
1
by: gzeng | last post by:
I am creating a singleton class in C++. Everything is fine except with the object pointers. I cannot instaniate a regular object. But I can define many pointers *aaaa, *bbbb, etc. to the class and...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.