Re: Ensure only one instance running

Greg Robinson <[email protected]> Wed, 26 Dec 2007 14:47:40 -0500
Newsgroups gmane.comp.windows.devel.dotnet.winforms
Message-ID <[email protected]>
A developer added the below to our app to address this:

'added for 1063, to make sure only one instance of the app runs at a time
      Dim objMutex As Mutex = New Mutex(False, "SINGLE_INSTANCE_APP_MUTEX")
      If objMutex.WaitOne(0, False) = False Then

         Dim pInstance As Process = _
             GetRunningInstance(Process.GetCurrentProcess().ProcessName)

         AlertBox.Show("An instance of AM.Net is already running on this
machine.  The current instance of AM.Net will be activated.", "AM.Net
Already Running", _
             MessageBoxButtons.OK, MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1, _
             ContentAlignment.MiddleLeft)

         'switch to current instance, kill new one
         Dim handle As IntPtr = pInstance.MainWindowHandle
         If Not IntPtr.Zero.Equals(handle) Then
            Win32Helper.ShowWindow(handle, 3)
            Win32Helper.SetForegroundWindow(handle)
         End If

         Application.ExitThread()
         objMutex.Close()
         objMutex = Nothing
         End

      End If


Greg Robinson
Custom Data Systems, Inc.
www.cds-am.net

-----Original Message-----
From: Discussion forum for developers using Windows Forms to build apps
and controls [mailto:[email protected]]On Behalf Of
Nassar, Anthony
Sent: Friday, December 21, 2007 10:00 AM
To: [email protected]
Subject: Re: [DOTNET-WINFORMS] Ensure only one instance running


Chris Sells's Windows Forms Programming 2.0 has a complete breakdown of
the different options here (Excel-style single instance, Word-style
single instance, etc.).