Ok, so you need a single instance of an application to run; what do you do? The obvious thing is to use a Mutex. Problem: Mutex can be left UNRELEASED by the framework depending how an application is closed. End result? An instance of your application holing on to the Mutex with no UI (Known in the Unix world as a Zombie).
Well, I figured a way around the problem:
Imports System.IO
Public Class Semaphore Private Shared _hnd As Semaphore = New Semaphore Private ReadOnly Property Filename() As String Get Dim s As New StackTrace Return Path.Combine(Path.GetTempPath(), _ s.GetFrame(4).GetMethod().DeclaringType.FullName.Replace(".", "_") & _ ".TMP") End Get End Property
Public Shared Function GetInstance() As Semaphore Return _hnd End Function
Private _sem As FileStream
Private Sub New() Try Me._sem = File.OpenWrite(Me.Filename) Catch ex As Exception Me._sem = Nothing End Try End Sub
Public ReadOnly Property Owned() As Boolean Get If Not Me._sem Is Nothing Then Return True Return False End Get End Property
Protected Overrides Sub Finalize() MyBase.Finalize() Me._sem.Close() Try File.Delete(Filename) Catch ex As Exception End Try End Sub
End Class
Filed under: Uncategorized