3 Replies Latest reply on Oct 9, 2015 10:09 AM by Peter McWha

    How to Check If App Already Running in VB.NET CF

    Peter McWha

      The interface for starting applications makes it easy for users to accidentally start multiple copies of the same application.

       

      How does a VB.NET application using the compact framework determine if there is another copy of itself running?  The System.Diagnostic.Process namespace is practically useless.

        • Re: How to Check If App Already Running in VB.NET CF
          Pietro Francesco Maggi

          Hi Peter,

          Usually you use a Mutex to have a single instance of your app running.

           

          When you app your launch, it checks if your Mutex is already defined. If yes, there's another instance running that you can bring in foreground and close the second one.

          If no mutex exist, you create it and keep running.

           

          You can find samples on internet mainly in C# on how to do this:

          .NET Reference Guide | Creating a Single-Instance Program | InformIT

          c# - What is the best way to make a single instance application in Compact Framework? - Stack Overflow

           

          ~Pietro

            • Re: How to Check If App Already Running in VB.NET CF
              Peter McWha

              Pietro:

               

              Thanks for participating.

               

              I was looking for a managed code solution.

               

              The compact framework does not support named mutexes.

                • Re: How to Check If App Already Running in VB.NET CF
                  Peter McWha

                  Here is my working VB.NET in CF based on the link Pietro referenced:

                   

                  #Region "p/invoke"

                  <DllImport("coredll.dll", EntryPoint:="CreateMutex", SetLastError:=True)> Private Function CreateMutex(parm00 As IntPtr, parm01 As Boolean, parm02 As String) As IntPtr

                  End Function

                  <DllImport("coredll.dll", EntryPoint:="ReleaseMutex", SetLastError:=True)> Private Function ReleaseMutex(parm00 As IntPtr) As IntPtr

                  End Function

                  #End Region

                  #Region "Internal Constants"

                  Private Const mainBFALSE As Boolean = False

                  Private Const mainBTRUE As Boolean = True

                  Private Const mainMUTEXINUSE As Int32 = 183

                  Private Const mainMUTEXUNIQUEID As String = "Your unique string here"

                  #End Region

                  #Region "Internal Variables"

                  Private mainmutexid As IntPtr

                  #End Region

                  Private Function mainMUTEXAVAILABLE() As Boolean

                      mainmutexid = IntPtr.Zero

                      mainmutexid = CreateMutex(IntPtr.Zero, mainBTRUE, mainMUTEXUNIQUEID)

                      If mainmutexid = IntPtr.Zero Then

                          mainMUTEXAVAILABLE = mainBTRUE

                      Else

                          If System.Runtime.InteropServices.Marshal.GetLastWin32Error() = mainMUTEXINUSE Then

                              mainMUTEXAVAILABLE = mainBFALSE

                          Else

                              mainMUTEXAVAILABLE = mainBTRUE

                          End If

                      End If

                  End Function

                  Private Sub mainMUTEXRELEASE()

                      If mainmutexid <> IntPtr.Zero Then

                          mainmutexid = ReleaseMutex(mainmutexid)

                      End If

                  End Sub

                   

                  The function mainMUTEXAVAILABLE returns true if the current instance is the only instance.