65.9K
CodeProject is changing. Read more.
Home

Single Instance of Application

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.52/5 (18 votes)

Oct 13, 2003

viewsIcon

92113

Shows a very generic way to find out if another instance of the application is already running and activate the running instance's main window by bringing it to the foreground.

Introduction

Brief preamble: Some time ago I was looking for a way to find out if another instance of the application is already running. A trivial task as you may say and I would agree with you instantly as long as you are familiar with Win32 API. I looked around on various sites including this one and found a few interesting articles, including an article talking at length how to set up a well-known object to pass command line parameters to an existing instance (a good DDE replacement), which is nice but not exactly what I was looking for. I needed something quick and dirty and so here it goes.

Public NotInheritable Class Win32Helper 
<System.Runtime.InteropServices.DllImport("user32.dll", _ 
EntryPoint:="SetForegroundWindow", _
CallingConvention:= Runtime.InteropServices.CallingConvention.StdCall, _
CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _
Public Shared Function _ 
     SetForegroundWindow(ByVal handle As IntPtr) As Boolean
' Leave function empty
End Function

<System.Runtime.InteropServices.DllImport("user32.dll", _ 
EntryPoint:="ShowWindow", _
CallingConvention:=Runtime.InteropServices.CallingConvention.StdCall, _
CharSet:=Runtime.InteropServices.CharSet.Unicode, SetLastError:=True)> _
Public Shared Function ShowWindow(ByVal handle As IntPtr, _ 
                             ByVal nCmd As Int32) As Boolean
' Leave function empty 
End Function

End Class ' End Win32Helper 

Public Shared Function GetRunningInstance(ByVal _ 
                   processName As String) As Process
    Dim proclist() As Process = _ 
       Process.GetProcessesByName(processName)
    For Each p As Process In proclist
        If p.Id <> Process.GetCurrentProcess().Id Then
            Return p
        End If
    Next
    Return Nothing
End Function

'The main entry point for the application
Public Shared Sub Main()
    Dim pInstance As Process = _ 
      Win32Helper.GetRunningInstance(Process.GetCurrentProcess().ProcessName)
    If Not pInstance Is Nothing Then
        Dim handle As IntPtr = pInstance.MainWindowHandle
        If Not IntPtr.Zero.Equals(handle) Then
            Win32Helper.ShowWindow(handle, 1)
            Win32Helper.SetForegroundWindow(handle)
        End If
        Application.ExitThread()
    Else
        Dim context As ApplicationContext = _ 
           New ApplicationContext(New MainFrame) 
        Application.Run(context)
    End If
End Sub

Few last comments: I had to resort to a no-so-graceful approach (as compared to using remoting) as needed to activate a main window of the running instance if one was found. Mutexes wouldn't help either.

For those on a look out for any trace plagiarism I should perhaps add, this is my adaptation of already well known methods. This is just my humble contribution for others who may find this all somewhat useful.