Detecting a Previous Instance
Detecting a Previous Instance
Detecting a Previous Instance
Detecting a Previous Instance
Detecting a Previous Instance
Detecting a Previous Instance Detecting a Previous Instance Detecting a Previous Instance Detecting a Previous Instance Detecting a Previous Instance Detecting a Previous Instance Detecting a Previous Instance Detecting a Previous Instance
Detecting a Previous Instance Detecting a Previous Instance
Detecting a Previous Instance
Go Back  Xtreme Visual Basic Talk > > > > Detecting a Previous Instance


Reply
 
Thread Tools Display Modes
  #1  
Old 12-17-2003, 08:44 PM
reboot's Avatar
rebootDetecting a Previous Instance reboot is offline
Keeper of foo

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Graceland
Posts: 15,614
Default Detecting a Previous Instance


A class to determine if your application is already running:

Code:
Public Class PreviousInstance Private _running As Boolean 'local 'this property gets set to True if there is a previous instance detected Public Property isRunning() As Boolean Get isRunning = _running End Get Set(ByVal Value As Boolean) _running = Value End Set End Property Public Sub New() 'get the current process Dim thisProcess As Process = Process.GetCurrentProcess() 'get collection of all processes Dim allProcesses As Process() = Process.GetProcessesByName(thisProcess.ProcessName) 'loop through running processes Dim p As Process For Each p In allProcesses 'ignore this process If p.Id <> thisProcess.Id Then 'make sure it is running from the exe file If System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", "\") _ = thisProcess.MainModule.FileName Then 'there is an instance already running, set our flag True and return _running = True Return End If End If Next 'no other instance _running = False End Sub End Class
Sample usage:
Code:
Dim p As New PreviousInstance If p.isRunning Then MessageBox.Show("Previous instance detected!") Return End If

Last edited by reboot; 12-17-2003 at 10:00 PM. Reason: Fixed typo.
Reply With Quote
  #2  
Old 03-25-2006, 08:52 AM
Mike Rosenblum's Avatar
Mike Rosenblum Mike Rosenblum is offline
Microsoft Excel MVP

Forum Leader
* Guru *
 
Join Date: Jul 2003
Location: New York, NY, USA
Posts: 7,848
Default

Here's another approach that I think also appears to work cleanly:
Code:
''' <summary> ''' Tests whether an instance of the currently running Application is already running. ''' </summary> ''' <returns> ''' Returns 'True' if an instance of the current Application is already running. ''' </returns> ''' <remarks> ''' No parameters are passed into the function. IsAlreadyRunning() relies ''' on the Process.GetCurrentProcess() to determine if other Applications of the ''' same name and path location are already running. ''' </remarks> ''' <Author> Mike_R on xtremevbtalk.com </Author> ''' <Date> 2006.03.25 </Date> Public Shared Function IsAlreadyRunning() As Boolean Dim thisProcess As Process = Process.GetCurrentProcess Dim moduleName As String = thisProcess.MainModule.ModuleName Dim processName As String = System.IO.Path.GetFileNameWithoutExtension(moduleName) For Each p As Process In Process.GetProcessesByName(processName) If p.Id <> thisProcess.Id AndAlso _ p.MainModule.FileName = thisProcess.MainModule.FileName Then Return True End If Next p End Function
-- Mike
__________________
My Articles:
| Excel from .NET | Excel RibbonX using VBA | Excel from VB6 | CVErr in .NET | MVP |
Avatar by Lebb

Last edited by Mat Sumpter; 04-25-2007 at 01:33 PM.
Reply With Quote
  #3  
Old 03-28-2006, 07:17 PM
shaul_ahuvaDetecting a Previous Instance shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

And another one, this time using a mutex.

Code:
Imports System.Threading ''' <summary> ''' Manages application instances ''' </summary> ''' <remarks></remarks> Public Class InstanceManager ''' <summary> ''' Keeps a reference to the created mutex. ''' </summary> ''' <remarks></remarks> Private Shared CurrentInstance As InstanceInfo #Region "InstanceInfo" ''' <summary> ''' Functions as a wrapper for the application instance mutex. ''' </summary> ''' <remarks>The ApplicationName and MultiUser fields make up the mutex name.</remarks> Private Class InstanceInfo Implements System.IDisposable Private ApplicationName As String Private InstanceMutex As Mutex Private IsDisposed As Boolean Private MultiUser As Boolean ''' <summary> ''' Initializes a new instance of the <see cref="InstanceInfo">InstanceInfo</see> class. ''' </summary> ''' <param name="instanceMutex">The <see cref="System.Threading.Mutex">Mutex</see> used ''' to keep track of application instances.</param> ''' <param name="applicationName">The application name.</param> ''' <param name="multiUser">Indicates if the mutex is local or global.</param> ''' <remarks></remarks> Friend Sub New(ByVal instanceMutex As System.Threading.Mutex, ByVal applicationName As String, _ ByVal multiUser As Boolean) Me.ApplicationName = applicationName Me.InstanceMutex = instanceMutex Me.MultiUser = multiUser GC.KeepAlive(Me) End Sub ''' <summary> ''' Releases the mutex. ''' </summary> ''' <remarks></remarks> Protected Overrides Sub Finalize() Me.Dispose() End Sub ''' <summary> ''' Returns if the specified application mutex is already defined. ''' </summary> ''' <param name="applicationName">The application name.</param> ''' <param name="multiUser">Indicates if the request is for a local or global mutex.</param> ''' <returns></returns> ''' <remarks></remarks> Friend Function HasMutex(ByVal applicationName As String, ByVal multiUser As Boolean) As Boolean Return Me.ApplicationName = applicationName AndAlso Me.MultiUser = multiUser End Function #Region "Dispose" ''' <summary> ''' Disposes of resources. ''' </summary> ''' <remarks></remarks> Public Sub Dispose() Implements IDisposable.Dispose Me.Dispose(True) GC.SuppressFinalize(Me) End Sub ''' <summary> ''' Disposes of resources. ''' </summary> ''' <param name="disposing"></param> ''' <remarks></remarks> Protected Overridable Sub Dispose(ByVal disposing As Boolean) If Not Me.IsDisposed Then If disposing Then End If Me.InstanceMutex.Close() Me.InstanceMutex = Nothing End If Me.IsDisposed = True End Sub #End Region End Class #End Region ''' <summary> ''' Initializes a new instance of the <see cref="InstanceManager">InstanceManager</see> class. ''' </summary> ''' <remarks>Prevent external instantiation.</remarks> Private Sub New() End Sub ''' <summary> ''' Registers the current application instance as the first instance for the local user only. ''' </summary> ''' <returns>Flag indicating if this application instance is the first instance.</returns> ''' <remarks></remarks> Public Shared Function RegisterFirstInstance() As Boolean Return InstanceManager.RegisterFirstInstance(True) End Function ''' <summary> ''' Registers the current application instance as the first instance. ''' </summary> ''' <param name="multiUser">Indicates if the application can be run by more than one user at a time.</param> ''' <returns>Flag indicating if this application instance is the first instance.</returns> ''' <remarks></remarks> Public Shared Function RegisterFirstInstance(ByVal multiUser As Boolean) As Boolean Dim appName As String = Process.GetCurrentProcess().ProcessName If InstanceManager.CurrentInstance Is Nothing OrElse _ Not InstanceManager.CurrentInstance.HasMutex(appName, multiUser) Then Dim first As Boolean = False Dim m As Mutex If Not multiUser Then 'Create the mutex in the global namespace so that only one user can run the app at one time. 'This will throw an exception in additional instances. Use ACLs in .NET 2.0 to mitigate this. Try m = New Mutex(False, "Global\" & appName, first) Catch ex As Exception Return False End Try Else 'Create the mutex in the local namespace so that every logged on user can run the app. m = New Mutex(False, "Local\" & appName, first) End If If first Then 'Get rid of the existing mutex if it exists (different namespace). If Not InstanceManager.CurrentInstance Is Nothing Then InstanceManager.CurrentInstance.Dispose() End If 'Keep the mutex instance around until the shared object is finalized. InstanceManager.CurrentInstance = New InstanceInfo(m, appName, multiUser) Else 'This isn't the first instance. m.Close() Return False End If End If Return True End Function End Class

Usage:
Code:
If Not InstanceManager.RegisterFirstInstance() Then MessageBox.Show("Only one instance can be running for a user at a time!") Application.Exit() End If

This code works in .NET 1.1 and 2.0, and supports previous instance detection for the current user session or the entire system (all user sessions).
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Terminating a previous instance of a VB program topspin General 5 11-20-2003 01:30 PM
Maximizing previous instance of my app BostjanK General 6 08-18-2003 05:44 AM
Previous and current version instance check? bkbig General 3 02-15-2003 10:46 AM
Can not always detect Previous Instance Neptune General 4 04-12-2001 08:37 AM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Detecting a Previous Instance
Detecting a Previous Instance
Detecting a Previous Instance Detecting a Previous Instance
Detecting a Previous Instance
Detecting a Previous Instance
Detecting a Previous Instance Detecting a Previous Instance Detecting a Previous Instance Detecting a Previous Instance Detecting a Previous Instance Detecting a Previous Instance Detecting a Previous Instance
Detecting a Previous Instance
Detecting a Previous Instance
 
Detecting a Previous Instance
Detecting a Previous Instance
 
-->