Solved

vb.net singleton pattern block threads

Posted on 2014-11-04
4
413 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 40422677
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
ID: 40422686
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 34

Accepted Solution

by:
it_saige earned 500 total points
ID: 40422766
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
ID: 40422776
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

623 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