Solved

vb.net singleton pattern block threads

Posted on 2014-11-04
4
371 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
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 33

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Modal Popup Extender control 1 22
VB.Net Tranfer data between to VB.Net apps 8 26
location of a form 2 14
How to duplicate form in Visual Studio 2015 2 16
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

803 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