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?