Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

vb.net singleton pattern block threads

Posted on 2014-11-04
4
Medium Priority
?
439 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 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 35

Accepted Solution

by:
it_saige earned 2000 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

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 …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

824 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