By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,882 Members | 2,489 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,882 IT Pros & Developers. It's quick & easy.

Open two instances of a windows application?

P: n/a
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I know
programmatically if the application is already opened when user tries to open
the second instance? Is there a way to prohibit user to launch more than one
application at one time?

Thanks.

Nina
Nov 21 '05 #1
Share this Question
Share on Google+
22 Replies


P: n/a
Hi Nina,

Each instance runs in its own address space, so, no, the global variables
will not interfere with each other.

HTH,

Bernie Yaeger

"Nina" <Ni**@discussions.microsoft.com> wrote in message
news:16**********************************@microsof t.com...
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I
know
programmatically if the application is already opened when user tries to
open
the second instance? Is there a way to prohibit user to launch more than
one
application at one time?

Thanks.

Nina

Nov 21 '05 #2

P: n/a
Hi Nina,

Each instance runs in its own address space, so, no, the global variables
will not interfere with each other.

HTH,

Bernie Yaeger

"Nina" <Ni**@discussions.microsoft.com> wrote in message
news:16**********************************@microsof t.com...
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I
know
programmatically if the application is already opened when user tries to
open
the second instance? Is there a way to prohibit user to launch more than
one
application at one time?

Thanks.

Nina

Nov 21 '05 #3

P: n/a

"Nina" <Ni**@discussions.microsoft.com> wrote in message
news:16**********************************@microsof t.com...
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I know programmatically if the application is already opened when user tries to open the second instance? Is there a way to prohibit user to launch more than one application at one time?


I am also interested in knowing if my application is already running when it
starts up.

This is especially helpful if a user opens a data file to which my app is
the default handler. How can I hand the new datafile to the already running
instance of my app instead of spawning a second copy of my app?
Nov 21 '05 #4

P: n/a

"Nina" <Ni**@discussions.microsoft.com> wrote in message
news:16**********************************@microsof t.com...
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I know programmatically if the application is already opened when user tries to open the second instance? Is there a way to prohibit user to launch more than one application at one time?


I am also interested in knowing if my application is already running when it
starts up.

This is especially helpful if a user opens a data file to which my app is
the default handler. How can I hand the new datafile to the already running
instance of my app instead of spawning a second copy of my app?
Nov 21 '05 #5

P: n/a
You can tell if an app is already running by using mutexes. Just search
this group for mutex. I've also read about ways to bring the other instance
to the forground or make it flash on the toolbar.

http://groups-beta.google.com/group/...t.languages.vb

As far as passing data between instances, that (IMO) is much more difficult.
I think I've read here that you can use ports or some such to accomplish
that trick.

Here is the boilerplate code I use:

' code to start app...
Try
SingletonApp.Run(New MyMainForm, "8ca35a66-6e9a-41d4-a87d-d9755b1f88c4")
'arbitrary GUID
Catch ex As SingletonException
MsgBox("Can not start because a previous instance of this application is
already running!")
End Try

' code inside singletonapp.vb file
Option Strict On

Imports System.Threading

Public Class SingletonApp

Private Shared _guid As String

Shared m_Mutex As Mutex
Public Shared Sub Run(ByVal mainForm As Form, ByVal guid As String)
_guid = guid
If (IsFirstInstance()) Then
AddHandler Application.ApplicationExit, AddressOf OnExit
Application.Run(mainForm)
Else
Throw New SingletonException
End If
End Sub
Public Shared Function IsFirstInstance() As Boolean
m_Mutex = New Mutex(False, _guid)
Dim owned As Boolean = False
owned = m_Mutex.WaitOne(TimeSpan.Zero, False)
Return owned
End Function

Public Shared Sub OnExit(ByVal sender As Object, ByVal args As
EventArgs)
m_Mutex.ReleaseMutex() ' think this may not be necessary???
m_Mutex.Close()
End Sub
End Class

Public Class SingletonException

Inherits System.ApplicationException

Public Sub New()
MyBase.New("Program already running!")
End Sub

Public Sub New(ByVal InnerException As Exception)
MyBase.New("Program already running!", InnerException)
End Sub

End Class

HTH,
Greg

"Noozer" <do*******@me.here> wrote in message
news:Oc**************@TK2MSFTNGP11.phx.gbl...

"Nina" <Ni**@discussions.microsoft.com> wrote in message
news:16**********************************@microsof t.com...
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I

know
programmatically if the application is already opened when user tries to

open
the second instance? Is there a way to prohibit user to launch more than

one
application at one time?


I am also interested in knowing if my application is already running when
it
starts up.

This is especially helpful if a user opens a data file to which my app is
the default handler. How can I hand the new datafile to the already
running
instance of my app instead of spawning a second copy of my app?

Nov 21 '05 #6

P: n/a
You can tell if an app is already running by using mutexes. Just search
this group for mutex. I've also read about ways to bring the other instance
to the forground or make it flash on the toolbar.

http://groups-beta.google.com/group/...t.languages.vb

As far as passing data between instances, that (IMO) is much more difficult.
I think I've read here that you can use ports or some such to accomplish
that trick.

Here is the boilerplate code I use:

' code to start app...
Try
SingletonApp.Run(New MyMainForm, "8ca35a66-6e9a-41d4-a87d-d9755b1f88c4")
'arbitrary GUID
Catch ex As SingletonException
MsgBox("Can not start because a previous instance of this application is
already running!")
End Try

' code inside singletonapp.vb file
Option Strict On

Imports System.Threading

Public Class SingletonApp

Private Shared _guid As String

Shared m_Mutex As Mutex
Public Shared Sub Run(ByVal mainForm As Form, ByVal guid As String)
_guid = guid
If (IsFirstInstance()) Then
AddHandler Application.ApplicationExit, AddressOf OnExit
Application.Run(mainForm)
Else
Throw New SingletonException
End If
End Sub
Public Shared Function IsFirstInstance() As Boolean
m_Mutex = New Mutex(False, _guid)
Dim owned As Boolean = False
owned = m_Mutex.WaitOne(TimeSpan.Zero, False)
Return owned
End Function

Public Shared Sub OnExit(ByVal sender As Object, ByVal args As
EventArgs)
m_Mutex.ReleaseMutex() ' think this may not be necessary???
m_Mutex.Close()
End Sub
End Class

Public Class SingletonException

Inherits System.ApplicationException

Public Sub New()
MyBase.New("Program already running!")
End Sub

Public Sub New(ByVal InnerException As Exception)
MyBase.New("Program already running!", InnerException)
End Sub

End Class

HTH,
Greg

"Noozer" <do*******@me.here> wrote in message
news:Oc**************@TK2MSFTNGP11.phx.gbl...

"Nina" <Ni**@discussions.microsoft.com> wrote in message
news:16**********************************@microsof t.com...
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I

know
programmatically if the application is already opened when user tries to

open
the second instance? Is there a way to prohibit user to launch more than

one
application at one time?


I am also interested in knowing if my application is already running when
it
starts up.

This is especially helpful if a user opens a data file to which my app is
the default handler. How can I hand the new datafile to the already
running
instance of my app instead of spawning a second copy of my app?

Nov 21 '05 #7

P: n/a
"Nina" <Ni**@discussions.microsoft.com> schrieb:
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application?
No, it won't.
How can I know programmatically if the application is already opened when
user tries to open
the second instance? Is there a way to prohibit user to launch more than
one
application at one time?


How do I make sure that only one instance of my application runs at a time?
<URL:http://www.yoda.arachsys.com/csharp/faq/#one.application.instance>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #8

P: n/a
"Nina" <Ni**@discussions.microsoft.com> schrieb:
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application?
No, it won't.
How can I know programmatically if the application is already opened when
user tries to open
the second instance? Is there a way to prohibit user to launch more than
one
application at one time?


How do I make sure that only one instance of my application runs at a time?
<URL:http://www.yoda.arachsys.com/csharp/faq/#one.application.instance>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #9

P: n/a

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:ux**************@TK2MSFTNGP14.phx.gbl...
You can tell if an app is already running by using mutexes. Just search
this group for mutex. I've also read about ways to bring the other instance to the forground or make it flash on the toolbar.

http://groups-beta.google.com/group/...t.languages.vb

As far as passing data between instances, that (IMO) is much more difficult. I think I've read here that you can use ports or some such to accomplish
that trick.

Here is the boilerplate code I use:


<snip>

Thanks!
Nov 21 '05 #10

P: n/a

"Greg Burns" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message
news:ux**************@TK2MSFTNGP14.phx.gbl...
You can tell if an app is already running by using mutexes. Just search
this group for mutex. I've also read about ways to bring the other instance to the forground or make it flash on the toolbar.

http://groups-beta.google.com/group/...t.languages.vb

As far as passing data between instances, that (IMO) is much more difficult. I think I've read here that you can use ports or some such to accomplish
that trick.

Here is the boilerplate code I use:


<snip>

Thanks!
Nov 21 '05 #11

P: n/a
Nak
Hey Noozer,

Use remoting to make a single instance application. It's pretty easy to
do,

http://www.codeproject.com/vb/net/sing_inistan.asp

Nick.
Nov 21 '05 #12

P: n/a
Nak
Hey Noozer,

Use remoting to make a single instance application. It's pretty easy to
do,

http://www.codeproject.com/vb/net/sing_inistan.asp

Nick.
Nov 21 '05 #13

P: n/a
Nina,

The in my eyes most simple one, disadvantage, it does not start another
program with the same name as well not.
\\\
Private mut As New Threading.Mutex(True, "myProgramName", mutCreated)
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not mutCreated Then Me.Close()
'etc
End Sub
///
I hope this helps?

Cor

"Nina" <Ni**@discussions.microsoft.com>
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I
know
programmatically if the application is already opened when user tries to
open
the second instance? Is there a way to prohibit user to launch more than
one
application at one time?

Thanks.

Nina

Nov 21 '05 #14

P: n/a
Nina,

The in my eyes most simple one, disadvantage, it does not start another
program with the same name as well not.
\\\
Private mut As New Threading.Mutex(True, "myProgramName", mutCreated)
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not mutCreated Then Me.Close()
'etc
End Sub
///
I hope this helps?

Cor

"Nina" <Ni**@discussions.microsoft.com>
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I
know
programmatically if the application is already opened when user tries to
open
the second instance? Is there a way to prohibit user to launch more than
one
application at one time?

Thanks.

Nina

Nov 21 '05 #15

P: n/a
Nak
> Hey Noozer,

My "appologees", I meant Nina
Nov 21 '05 #16

P: n/a
Nak
> Hey Noozer,

My "appologees", I meant Nina
Nov 21 '05 #17

P: n/a
Thank you.

"Cor Ligthert" wrote:
Nina,

The in my eyes most simple one, disadvantage, it does not start another
program with the same name as well not.
\\\
Private mut As New Threading.Mutex(True, "myProgramName", mutCreated)
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not mutCreated Then Me.Close()
'etc
End Sub
///
I hope this helps?

Cor

"Nina" <Ni**@discussions.microsoft.com>
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I
know
programmatically if the application is already opened when user tries to
open
the second instance? Is there a way to prohibit user to launch more than
one
application at one time?

Thanks.

Nina


Nov 21 '05 #18

P: n/a
Thank you.

"Cor Ligthert" wrote:
Nina,

The in my eyes most simple one, disadvantage, it does not start another
program with the same name as well not.
\\\
Private mut As New Threading.Mutex(True, "myProgramName", mutCreated)
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
If Not mutCreated Then Me.Close()
'etc
End Sub
///
I hope this helps?

Cor

"Nina" <Ni**@discussions.microsoft.com>
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I
know
programmatically if the application is already opened when user tries to
open
the second instance? Is there a way to prohibit user to launch more than
one
application at one time?

Thanks.

Nina


Nov 21 '05 #19

P: n/a
Thank you.

"Nak" wrote:
Hey Noozer,


My "appologees", I meant Nina

Nov 21 '05 #20

P: n/a
Thank you.

"Nak" wrote:
Hey Noozer,


My "appologees", I meant Nina

Nov 21 '05 #21

P: n/a
Thank you all for your valuable inputs.

"Nina" wrote:
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I know
programmatically if the application is already opened when user tries to open
the second instance? Is there a way to prohibit user to launch more than one
application at one time?

Thanks.

Nina

Nov 21 '05 #22

P: n/a
Thank you all for your valuable inputs.

"Nina" wrote:
While user is working on a windows application, at same time he or she
launches another instance of the same application. Will this affect the
global variables in the first instance of this application? How can I know
programmatically if the application is already opened when user tries to open
the second instance? Is there a way to prohibit user to launch more than one
application at one time?

Thanks.

Nina

Nov 21 '05 #23

This discussion thread is closed

Replies have been disabled for this discussion.