Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. [VB.NET 2008] Compact Framework: how to start a single instance of an application

[VB.NET 2008] Compact Framework: how to start a single instance of an application

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharphelpvisual-studiotutorial
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    steve_9496613
    wrote on last edited by
    #1

    Hi everybody, my application is written in VB.NET using Visual Studio 2008 and it runs an a Windows CE device, so it uses Compact Framework. I need to avoid multiple instances of the application and I don't know how to do it. I have found some solutions but only for the full framework, not for the compact. Other solutions to this problem talk about mutex: when the application start I should create a mutex if this is the first instance of the application or stop the application if the mutex already exists (or something like this)... but I don't know how to do this. Can someone help me? Thanks in advance

    L 1 Reply Last reply
    0
    • S steve_9496613

      Hi everybody, my application is written in VB.NET using Visual Studio 2008 and it runs an a Windows CE device, so it uses Compact Framework. I need to avoid multiple instances of the application and I don't know how to do it. I have found some solutions but only for the full framework, not for the compact. Other solutions to this problem talk about mutex: when the application start I should create a mutex if this is the first instance of the application or stop the application if the mutex already exists (or something like this)... but I don't know how to do this. Can someone help me? Thanks in advance

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      steve_9496613 wrote:

      but I don't know how to do this.

      Would require some P/Invokes as described on SO[^]. Also check out the alternative file-lock.

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      S 1 Reply Last reply
      0
      • L Lost User

        steve_9496613 wrote:

        but I don't know how to do this.

        Would require some P/Invokes as described on SO[^]. Also check out the alternative file-lock.

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        S Offline
        S Offline
        steve_9496613
        wrote on last edited by
        #3

        Hi Eddy! Searching again I found some code about mutex and with little changes it seems to work. I wrote (copy/past + some changes...) a class SingleInstanceApplication:

        Imports System.Reflection
        Imports System.Runtime.InteropServices

        Public Class SingleInstanceApplication

        _
        Public Shared Function CreateMutex(ByVal Attr As IntPtr, ByVal Own As Boolean, ByVal Name As String) As IntPtr
        End Function

        _
        Public Shared Function ReleaseMutex(ByVal hMutex As IntPtr) As Boolean
        End Function

        Const ERROR_ALREADY_EXISTS As Long = 183

        Public Shared Sub Run(ByVal frm As Form)
        Dim name As String = Assembly.GetExecutingAssembly().GetName().Name
        Dim mutexHandle As IntPtr = CreateMutex(IntPtr.Zero, True, name)
        Dim Rerror As Long = Marshal.GetLastWin32Error()

        'MessageBox.Show(name & " - " & mutexHandle.ToString & " - " & Rerror.ToString)  'just for debug
        
        If (Not Rerror = ERROR\_ALREADY\_EXISTS) Then
          Application.Run(frm)
        End If
        
        'MessageBox.Show("release mutex")  'just for debug
        
        ReleaseMutex(mutexHandle)
        

        End Sub
        End Class

        then I wrote a class to start the application:

        Public Class StartApp
        Public Shared Sub main()
        Try
        SingleInstanceApplication.Run(FormMain)
        Catch ex As Exception
        MessageBox.Show(ex.Message)
        End Try
        End Sub
        End Class

        finally I modified the start object of the application from "FormMain" (the main form of the application) to "StartApp". It works! Thanks.

        L 1 Reply Last reply
        0
        • S steve_9496613

          Hi Eddy! Searching again I found some code about mutex and with little changes it seems to work. I wrote (copy/past + some changes...) a class SingleInstanceApplication:

          Imports System.Reflection
          Imports System.Runtime.InteropServices

          Public Class SingleInstanceApplication

          _
          Public Shared Function CreateMutex(ByVal Attr As IntPtr, ByVal Own As Boolean, ByVal Name As String) As IntPtr
          End Function

          _
          Public Shared Function ReleaseMutex(ByVal hMutex As IntPtr) As Boolean
          End Function

          Const ERROR_ALREADY_EXISTS As Long = 183

          Public Shared Sub Run(ByVal frm As Form)
          Dim name As String = Assembly.GetExecutingAssembly().GetName().Name
          Dim mutexHandle As IntPtr = CreateMutex(IntPtr.Zero, True, name)
          Dim Rerror As Long = Marshal.GetLastWin32Error()

          'MessageBox.Show(name & " - " & mutexHandle.ToString & " - " & Rerror.ToString)  'just for debug
          
          If (Not Rerror = ERROR\_ALREADY\_EXISTS) Then
            Application.Run(frm)
          End If
          
          'MessageBox.Show("release mutex")  'just for debug
          
          ReleaseMutex(mutexHandle)
          

          End Sub
          End Class

          then I wrote a class to start the application:

          Public Class StartApp
          Public Shared Sub main()
          Try
          SingleInstanceApplication.Run(FormMain)
          Catch ex As Exception
          MessageBox.Show(ex.Message)
          End Try
          End Sub
          End Class

          finally I modified the start object of the application from "FormMain" (the main form of the application) to "StartApp". It works! Thanks.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Nice, you're welcome :)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups