Re: Ensure only one instance running

Dean Cleaver <[email protected]> Thu, 27 Dec 2007 17:23:22 +1300
Newsgroups gmane.comp.windows.devel.dotnet.winforms
Message-ID <4543A775711185429E59B18055D41674231FD4@sxslakl004.xceptionsoftware.com>
Only issue is that if every developer uses that code, then only one of
their apps can run at a time on the local machine. I tend to try to tie
the mutex to the application name or something so that multiple apps can
still run on the same machine.

Dino

-----Original Message-----
From: Discussion forum for developers using Windows Forms to build apps
and controls [mailto:[email protected]] On Behalf Of
Greg Robinson
Sent: Thursday, 27 December 2007 08:48
To: [email protected]
Subject: Re: [DOTNET-WINFORMS] Ensure only one instance running

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.).