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