View Single Post
 
Old 12-17-2003, 08:44 PM
reboot's Avatar
reboot 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