Is there a way to have a Shared instance of a Class in a DLL

J

JB

Hi All,

I'm building a DLL that can be used by several applications.
I'd like to have one single instance of a Class stored in my DLL that
would be ("shared") by all the applications using that DLL.

I've tried to declare in my DLL a Class with a single "Shared" member,
but each time I reference the DLL from another application, a new
instance is created.

For instance:

'My shared Class (in the DLL)
Public Class CSharedKernel
Public Shared MainKernel As New CKernel
End Class

'The content of the shared class (in the DLL)
Public Class CKernel
Public m_KernelString As String = ""

Sub New()
m_KernelString = "Kernel Object created at " &
Now.ToString("HH:mm:ss")
End Sub
End Class

'When I call the following in 2 separate applications using the DLL
'I get 2 different times telling me it's actually 2 different
'instances of CKernel
MsgBox(CSharedKernel.MainKernel.m_KernelString")

Is this possible to achieve at all in a DLL?
Thanks a lot
 
J

Jack Jackson

Each application has its own single copy of the class. There is no
way, just using VB apps, to have a global instance across mutiple
applications.

You might be able to do it with COM. A Windows Service would be
another possibility.
 
M

Michel Posseth [MCP]

There is no
way, just using VB apps, to have a global instance across mutiple
applications.

Actually there is a way ( both VB and C# support this technique )

You could use a singleton class in a remoting host , this acts exactly how
you describe it
this way you can access the class across application domains and even across
the network / internet

regards

Michel
 
M

Michel Posseth [MCP]

To make it a true singleton add this to the singleton class


#Region " Remoting Override "
''' -----------------------------------------------------------------------------
''' <summary>
''' this method will make sure that the leasetime will be infinite
''' </summary>
''' <returns>Nothing as valid lease point expiration time </returns>
''' <remarks>
''' </remarks>
''' <history>
''' [michel] 6/4/2005 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Overrides Function InitializeLifetimeService() As Object
Return Nothing
End Function
#End Region

If you omit this the singleton will have a lease time of 20 minutes or so

regards

Michel
 
J

JB

To make it a true singleton add this to the singleton class

#Region " Remoting Override "
''' -----------------------------------------------------------------------------
''' <summary>
''' this method will make sure that the leasetime will be infinite
''' </summary>
''' <returns>Nothing as valid lease point expiration time </returns>
''' <remarks>
''' </remarks>
''' <history>
''' [michel] 6/4/2005 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Overrides Function InitializeLifetimeService() As Object
Return Nothing
End Function
#End Region

If you omit this the singleton will have a lease time of 20 minutes or so

regards

Michel

Actually there is a way ( both VB and C# support this technique )
You could use a singleton class in a remoting host , this acts exactly how
you describe it
this way you can access the class across application domains and even
across the network / internet

Hi All,

Thanks very much for your quick responses.
After much reading and posting around the subject, I've come to the
conclusion that I'm gonna have to look into remoting.
I was trying to avoid it as much as possible to stick to things I
know, but I guess it's time for me to face the music and learn it.

Thanks again
Regards
JB
 
G

Guest

I'm building a DLL that can be used by several applications.
I'd like to have one single instance of a Class stored in my DLL that
would be ("shared") by all the applications using that DLL.

You could create it as a Web Service or expose a single object through
Remoting/WCF.
 

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