Singletons and Terminal server

A

Andrew Baker

I have some code to ensure a singleton which has worked perfectly for years.
I need to alter it to work such that each user on TerminalServices gets
their own instance of the application (and therefore each form). Can
someone offer a good solution? (and yes, I have googled but nothing obvious
came up)
Private Shared somePrivateStaticObject As New Object 'used for threadsafty
and locking

Private Shared myInstance As frmControl



Public Shared Function GetInstance() As frmControl

SyncLock (somePrivateStaticObject) ' make it threadsafe

If myInstance Is Nothing Then

myInstance = New frmControl

End If

End SyncLock

Return myInstance

End Function





I was thinking of changing the lock object to something like
Dim somePrivateStaticObject As String = "formName" +
loggedInUserName

Private myInstance As frmControl

but that throws an error of: "Cannot refer to an instance member of a class
from within a shared method or shared member initializer without an explicit
instance of the class."

as I dont yet have a class and the object is no longer shared.

Can someone suggest a way that works??

Thanks

Andrew.
 
C

Chuck D

Here is a generic implementation of a singleton that will manage objects of
any type, with one instance per 'UserName'.

Public Class UserSingleton(Of t As {New})
Private Shared m_t As Dictionary(Of String, t)

Shared Sub New()
m_t = New Dictionary(Of String, t)
End Sub

Public Shared Function getInstance(ByVal UserName As String) As t
SyncLock (m_t)
If m_t.ContainsKey(UserName) Then
Return m_t.Item(UserName)
Else
Dim userT As New t
m_t.Add(UserName, userT)
Return userT
End If
End SyncLock
End Function
End Class

Here is some code that tests the class:

Dim SteveJonesForm1 As Windows.Forms.Form = UserSingleton(Of
Windows.Forms.Form).getInstance("Steve Jones")
Dim BobWilliamsForm1 As Windows.Forms.Form = UserSingleton(Of
Windows.Forms.Form).getInstance("Bob Williams")

Dim SteveJonesForm2 As Windows.Forms.Form = UserSingleton(Of
Windows.Forms.Form).getInstance("Steve Jones")
Dim BobWilliamsForm2 As Windows.Forms.Form = UserSingleton(Of
Windows.Forms.Form).getInstance("Bob Williams")

' Test for instances
Console.WriteLine("SteveJonesForm1 Is Instantiated = " +
(SteveJonesForm1 IsNot Nothing).ToString)
Console.WriteLine("BobWilliamsForm1 Is Instantiated = " +
(BobWilliamsForm1 IsNot Nothing).ToString)
Console.WriteLine("SteveJonesForm2 Is Instantiated = " +
(SteveJonesForm2 IsNot Nothing).ToString)
Console.WriteLine("BobWilliamsForm2 Is Instantiated = " +
(BobWilliamsForm2 IsNot Nothing).ToString)

' Test instances for equality
Console.WriteLine("SteveJonesForm1 Is SteveJonesForm2 = " +
(SteveJonesForm1 Is SteveJonesForm2).ToString)
Console.WriteLine("BobWilliamsForm1 Is BobWilliamsForm2 = " +
(BobWilliamsForm1 Is BobWilliamsForm2).ToString)
Console.WriteLine("SteveJonesForm1 Is BobWilliamsForm1 = " +
(SteveJonesForm1 Is BobWilliamsForm1).ToString)
Console.WriteLine("SteveJonesForm2 Is BobWilliamsForm2 = " +
(SteveJonesForm2 Is BobWilliamsForm2).ToString)

I don't know if I would go down this road though :)
 
A

Andrew Baker

Thanks Chuck.
After posting I was thinking of using a dictionary with username as the key
but you implementation helped.

I was more interested in your comment "I don't know if I would go down this
road though :)"
CVould you extrrapolate on this? Why would you have concverns about this
approach?

Andrew.
 
C

Chuck D

Well, I guess i was thinking that you were going to manage *all* the forms in
the app this way, but that was probably a dumb assumption on my part :)

I was thinking another option would be to wrap a shell executable around
your app designed to launch the TS per user app instances of your real app.
Then you don't have to touch your main app codebase.

Chuck
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top