|
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
|
|
|
|
|
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
If you can't read my code, try converting it here[^]
|
|
|
|
|
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
<DllImport("Coredll.dll", SetLastError:=True)> _
Public Shared Function CreateMutex(ByVal Attr As IntPtr, ByVal Own As Boolean, ByVal Name As String) As IntPtr
End Function
<DllImport("Coredll.dll", SetLastError:=True)> _
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()
If (Not Rerror = ERROR_ALREADY_EXISTS) Then
Application.Run(frm)
End If
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.
|
|
|
|