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
Solved

vb.net singleton pattern block threads

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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