[VB.NET 2008] Compact Framework: how to start a single instance of an application
-
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
-
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
-
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.InteropServicesPublic 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 FunctionConst 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 Classthen 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 Classfinally I modified the start object of the application from "FormMain" (the main form of the application) to "StartApp". It works! Thanks.
-
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.InteropServicesPublic 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 FunctionConst 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 Classthen 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 Classfinally I modified the start object of the application from "FormMain" (the main form of the application) to "StartApp". It works! Thanks.