Solved

vb.net singleton pattern block threads

Posted on 2014-11-04
4
353 Views
Last Modified: 2014-11-04
I'm trying to implement the following singleton pattern in vb.net. In java i use the Synchronization directive to block theading invokation until the code block is cleared. In vb.net i've tried to use the synclock command but it requires an object referece first which kind of eliminates the benefit of the singleton pattern in the first place. What is vb.net equivalant to java's Synchronization keyword? Or is their some kind of other technique to achive this?

Public Class singleton

    Shared uniqueinstance As singleton

    Private Sub New()

    End Sub

    Public Shared Function getInstance() As singleton

        SyncLock uniqueinstance
            If uniqueinstance Is Nothing Then
                uniqueinstance = New singleton
            End If
        End SyncLock

        Return uniqueinstance
    End Function

End Class
0
Comment
Question by:glenn_r
  • 2
4 Comments
 
LVL 35

Expert Comment

by:Miguel Oz
Comment Utility
It seems OK as SyncLock has a similar functionality as it locks access to your protected code in a multithreaded application. Internally it uses the Monitor class as described here
0
 

Author Comment

by:glenn_r
Comment Utility
if you look at my pattern i'm trying to limit the class to a single instance
i can't use synclock as it requires a reference to the object and initially there is no object so i'm kids of deadlocked with synclock. is there some other way to block multiple threads from invoking my method?
0
 
LVL 32

Accepted Solution

by:
it_saige earned 500 total points
Comment Utility
When implementing a singleton object in .NET there are essentially two schools of thought.  One use a lock object:
Public Class Singleton
	Private Shared mSingletonLock As New Object()
	Private instance As Singleton = Nothing

	Private Sub New()
	End Sub

	Public Shared Function GetInstance() As Singleton
		SyncLock mSingletonLock
			If instance Is Nothing Then
				instance = New Singleton()
			End If
		End SyncLock
		Return instance
	End Function
End Class

Open in new window


Or

Let the CLR do the walking for you:
Public NotInheritable Class Singleton
	Private Sub New()
	End Sub

	Public Shared ReadOnly Instance as New Singleton
End Class

Open in new window


https://social.msdn.microsoft.com/Forums/en-US/a18448b1-3728-4a8a-a42d-69b4f57b835a/singleton-pattern-sharedsynclock-confusion?forum=vblanguage

Webcast

-saige-
0
 

Author Comment

by:glenn_r
Comment Utility
ya your right i could just just eagar loading to solve the problem but if i want to use lazy loading where the instance only gets created when/if its used then i was considering creating a dummy object called blocker and then just set the synklock to that. Dones the same thing. thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Paging GridView 7 32
How can I use this extension method? 8 35
Clone Visual studio 2013 solution 5 15
Hide Tab Page 3 18
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now