I haven't used singleton before, so maybe I have the complete wrong idea, but I thought the point was that it can only be initialized once, and anyone calling it will reference the same instance..?
So I took this from an example. GetInstance() is called from hundred different places in my program, and when I debug, the line "Prog = New Program" keeps getting hit for each of those calls. Which I thought was exactly what should NOT happen.. Or do I have some fundamental misunderstanding?
' ********************** THREAD SAFE SINGLETON **************************
Public Class Program
Private Shared Prog As Program = Nothing
Private Shared ReadOnly singletonLock As New Object()
Public Shared Function GetInstance() As Program
SyncLock singletonLock
If Prog Is Nothing Then
Prog = New Program
End If
Return Prog
End SyncLock
End Function
EDIT:
It seems the "New" sub triggers a number of calls to Program.GetInstance, before the first one completes. This is due to me earlier having lots of shared public objects in this class, which are no longer shared since the class was made singleton. And these objects, when initialized, calls the Program class for reference to other of it's objects.