Open two instances of a windows application?

G

Guest

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
 
N

Noozer

Nina said:
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?
 
B

Bernie Yaeger

Hi Nina,

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

HTH,

Bernie Yaeger
 
G

Greg Burns

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/microsoft.public.dotnet.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
 
H

Herfried K. Wagner [MVP]

Nina said:
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>
 
N

Noozer

Greg Burns said:
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/microsoft.public.dotnet.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!
 
C

Cor Ligthert

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
 
G

Guest

Thank you.

Cor Ligthert said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top