Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is app already running?

8 views
Skip to first unread message

Michael

unread,
Oct 22, 2003, 4:19:29 PM10/22/03
to
In VB.NET, how do you check to see if an instance of your
application is already running?

Herfried K. Wagner [MVP]

unread,
Oct 22, 2003, 4:31:54 PM10/22/03
to
* "Michael" <anon...@discussions.microsoft.com> scripsit:

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

<http://www.google.com/groups?selm=blgrft%24c51nq%244%40ID-208219.news.uni-berlin.de>

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Tom Shelton

unread,
Oct 22, 2003, 7:18:42 PM10/22/03
to
In article <bn6pg4$ubfg5$1...@ID-208219.news.uni-berlin.de>, Herfried K. Wagner [MVP] wrote:
> * "Michael" <anon...@discussions.microsoft.com> scripsit:
>> In VB.NET, how do you check to see if an instance of your
>> application is already running?
>
><http://www.google.com/groups?selm=blgrft%24c51nq%244%40ID-208219.news.uni-berlin.de>
>

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
MVP [Visual Basic]

Herfried K. Wagner [MVP]

unread,
Oct 23, 2003, 3:35:54 AM10/23/03
to
* Tom Shelton <t...@mtogden.com> scripsit:

>> <http://www.google.com/groups?selm=blgrft%24c51nq%244%40ID-208219.news.uni-berlin.de>
>
> 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.

;-)

Michael Passalacqua

unread,
Oct 23, 2003, 1:40:44 PM10/23/03
to
Thanks, you guys!

Michael Passalacqua
Portland Community College
CIS Faculty

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Tom Shelton

unread,
Oct 23, 2003, 2:10:40 PM10/23/03
to
In article <bn80mo$t9u6g$3...@ID-208219.news.uni-berlin.de>, Herfried K. Wagner [MVP] wrote:
> * Tom Shelton <t...@mtogden.com> scripsit:
>>> <http://www.google.com/groups?selm=blgrft%24c51nq%244%40ID-208219.news.uni-berlin.de>
>>
>> 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 :)

George Shubin

unread,
Oct 23, 2003, 2:34:57 PM10/23/03
to

"Michael" <anon...@discussions.microsoft.com> wrote in message
news:0b5301c398d9$cbeeb5d0$a001...@phx.gbl...

> 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 geo...@dxonline.com
------------------------------------------------------------------------


Herfried K. Wagner [MVP]

unread,
Oct 23, 2003, 3:17:39 PM10/23/03
to
* Tom Shelton <t...@mtogden.com> scripsit:
>>> 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.

Herfried K. Wagner [MVP]

unread,
Oct 23, 2003, 3:18:25 PM10/23/03
to
* "George Shubin" <d...@dxonline.com> scripsit:

>> 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

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.

Fergus Cooney

unread,
Oct 23, 2003, 7:33:46 PM10/23/03
to
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

Fergus Cooney

unread,
Oct 23, 2003, 7:35:24 PM10/23/03
to
Hi Herfried,

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

Regards,
Fergus


Herfried K. Wagner [MVP]

unread,
Oct 23, 2003, 8:41:04 PM10/23/03
to
* "Fergus Cooney" <fil...@post.com> scripsit:

> 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.

Fergus Cooney

unread,
Oct 23, 2003, 10:59:17 PM10/23/03
to
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


Tom Shelton

unread,
Oct 24, 2003, 12:28:46 AM10/24/03
to

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.

Fergus Cooney

unread,
Oct 24, 2003, 12:43:13 AM10/24/03
to
Hi Tom,

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

Regards,
Fergus


Herfried K. Wagner [MVP]

unread,
Oct 24, 2003, 9:30:56 AM10/24/03
to
* "Fergus Cooney" <fil...@post.com> scripsit:
> 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...

;-)

Fergus Cooney

unread,
Oct 24, 2003, 2:43:57 PM10/24/03
to
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


Herfried K. Wagner [MVP]

unread,
Oct 24, 2003, 5:10:51 PM10/24/03
to
* "Fergus Cooney" <fil...@post.com> scripsit:
> 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.

0 new messages