Saturday, December 20, 2003

I was recently working with a local software development house that was completing a new release of their application. During a meeting with their development manager they had a rather unique question. They wanted to ensure that only a single copy of their application was running at any time. They hadn’t been able to figure out a simple way to accomplish this. In working with them we created the following code snippet that we embedded in the application startup.

 

Dim proc() As Process

 

 'Determine the name of the current process

 Dim modulename As String = Process.GetCurrentProcess.MainModule.ModuleName

 Dim procname As String = System.IO.Path.GetFileNameWithoutExtension(modulename)

 

  ' retrieve any processes with this name

  proc = Process.GetProcessesByName(procname)

 

  If proc.Length > 1 Then

                 ' there is more than one process of this name - END

                MsgBox("Application currently running - only one instance allowed")

                 End

  End If


8:44:17 PM