|
|
|
VB6 developers can use the App.PrevInstance property to check whether there is another instance of the same process already running on the current machine. This property isn't available any longer in VB.NET, so you must retrieve this information by using the properties of the Process object.
The easiest way to do so is by means of the Process.GetProcessesByName shared method, which takes a process name and returns an array of all the processes with that name. The process name of an EXE application is usually equal to the name of the main EXE file but without the extension. So if your application is MyApp.Exe, its process name is "MyApp" and you can check whether there is already another instance of this process running with this code: |
The problem in writing a generic function that checks whether the current application is already running comes from the fact that the ProcessName property of the Process object seems to be limited to 15 characters, so longer process names are truncated. For example, create the default ConsoleApplication1 application and run this code: ' display the name of the current process Console.WriteLine(Process.GetCurrentProcess.ProcessName) ' => ConsoleApplicat
A safer way to retrieve a process name is to get the filename of its main module and dropping the extension. The following reusable routine uses this approach: |
Click here to copy the following block | Function AppIsAlreadyRunning() As Boolean Dim moduleName As String = Process.GetCurrentProcess.MainModule.ModuleName Dim procName As String = System.IO.Path.GetFileNameWithoutExtension _ (moduleName) If Process.GetProcessesByName(procName).Length > 1 Then Return True End If End Function |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|