Is app already running?

  • Thread starter Thread starter Michael
  • Start date Start date
M

Michael

In VB.NET, how do you check to see if an instance of your
application is already running?
 

That is a good way to do it if you need to get a bit of information
about the previous proces - especially if your going to pass information
to it. But if you have simple needs, like all you care about is if it
is running - here is an alternate method.

Imports System.Threading

....

Sub Main()
Dim owned As Boolean
Dim mut As New Mutex(True, "myuniquemutexname", owned)

If owned Then
Application.Run(New MainForm())
mut.ReleaseMutex()
Else
MessageBox.Show("A previous instance is already running")
End If

End Sub

Anyway, it is an alternative that I use a lot :)
 
* Tom Shelton said:

That is a good way to do it if you need to get a bit of information
about the previous proces - especially if your going to pass information
to it. But if you have simple needs, like all you care about is if it
is running - here is an alternate method. [...]
Dim mut As New Mutex(True, "myuniquemutexname", owned)

I remember I posted a link to a sample like this.

;-)
 
Thanks, you guys!

Michael Passalacqua
Portland Community College
CIS Faculty
 
* Tom Shelton said:

That is a good way to do it if you need to get a bit of information
about the previous proces - especially if your going to pass information
to it. But if you have simple needs, like all you care about is if it
is running - here is an alternate method. [...]
Dim mut As New Mutex(True, "myuniquemutexname", owned)

I remember I posted a link to a sample like this.

;-)

Crap! I hate when I don't scroll down! Sorry. I wasn't trying to take
away from your post Herfried... I was just trying to provide some
additional information (which I see was there, boy do I feel dumb :)
 
Michael said:
In VB.NET, how do you check to see if an instance of your
application is already running?

Here's another way: (watch out for word-wrap)

'This is the application's startup routine
'Check to see if a previous instance of this application is running already
If
(UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurren
tProcess.ProcessName)) > 0) Then
MessageBox.Show("Already running on your system.", AppDesc,
MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If


--
------------------------------------------------------------------------
George Shubin Custom Software Development
dX Software Systems Database Applications
Ph: 503-981-6806 Fax: 503-982-0120
www.dxonline.com (e-mail address removed)
------------------------------------------------------------------------
 
* Tom Shelton said:
That is a good way to do it if you need to get a bit of information
about the previous proces - especially if your going to pass information
to it. But if you have simple needs, like all you care about is if it
is running - here is an alternate method. [...]
Dim mut As New Mutex(True, "myuniquemutexname", owned)

I remember I posted a link to a sample like this.

;-)

Crap! I hate when I don't scroll down! Sorry. I wasn't trying to take
away from your post Herfried... I was just trying to provide some
additional information (which I see was there, boy do I feel dumb :)

No problem. I am lazy too.

;-)
 
* "George Shubin said:
Here's another way: (watch out for word-wrap)

'This is the application's startup routine
'Check to see if a previous instance of this application is running already
If
(UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurren
tProcess.ProcessName)) > 0) Then
MessageBox.Show("Already running on your system.", AppDesc,
MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If

Notice that this will return a wrong result if there are more than one
processes running on the system which have the same process name but
belong to different applications.
 
Hi Tom, Herfried,

Lazy, he calls him. What happened to forgetful, distracted, unobservant,
hasty?
I prefer hasty myself - as in keen to get back to post a solution ;-).

Actually there is a difference between your methods.

Dim FirstTimeIn As Boolean
Dim mut As New Mutex(True, "myuniquemutexname", FirstTimeIn)
If FirstTimeIn Then

versus
Dim mut As Mutex = New Mutex(False, "myuniquemutexname")
If mut.WaitOne(10, False) Then

Any advantages of one over the other?

Regards,
Fergus
 
Hi Herfried,

What's the difference between an application name and a process name?

Regards,
Fergus
 
* "Fergus Cooney said:
What's the difference between an application name and a process name?

Let's say two developers write two different programms with the name
"Foo". If the user installs both of them (they are different!) into
different locations, for example

"C:\Program Files\Foo1\Foo.exe"
"C:\Program Files\Foo2\Foo.exe"

and then starts both of them, the process name of both application
instances (instances of _different_ applications) will be the same.
When shutting down all applications with the same name maybe instances
of other applications are killed too.
 
Hi Herfried,

Thanks, that's what I believed, but I thought I might have missed
soimething.

I think it is unlikely in general to have the clash that you mention, but
I can see it happening with different versions of the same software or
utilities such as 'Calculator'.

Regards,
Fergus
 
Hi Tom, Herfried,

Lazy, he calls him. What happened to forgetful, distracted, unobservant,
hasty?
I prefer hasty myself - as in keen to get back to post a solution ;-).

Actually there is a difference between your methods.

Dim FirstTimeIn As Boolean
Dim mut As New Mutex(True, "myuniquemutexname", FirstTimeIn)
If FirstTimeIn Then

versus
Dim mut As Mutex = New Mutex(False, "myuniquemutexname")
If mut.WaitOne(10, False) Then

Any advantages of one over the other?

Regards,
Fergus

To be honest, I don't really think so. I have never used the method
Herfried showed - I've always just used the constructor to determine if
I have recieved ownership... But, it looks like 6's to me.
 
Hi Tom,

Thanks. I prefer the first version - it looks less like technobabble -
especially with the renamed ownership variable.

Regards,
Fergus
 
* "Fergus Cooney said:
Thanks, that's what I believed, but I thought I might have missed
soimething.

I think it is unlikely in general to have the clash that you mention, but
I can see it happening with different versions of the same software or
utilities such as 'Calculator'.

That's exaclty what I wanted to say...

;-)
 
Hi Herfried,

Are you related to the Herfried who I've been mud-wrestling with over in
the other threads?

You seem like such a nice chap!! ;-)

Regards,
Fergus
 
* "Fergus Cooney said:
Are you related to the Herfried who I've been mud-wrestling with over in
the other threads?

You seem like such a nice chap!! ;-)

Are you the Fergus I wanted to plonk? Maybe there are two different
"Fergus Cooney" posting to this group.

;-)
 
Back
Top