Single Instance of a class/DLL

AndyDale

Member
Joined
Nov 13, 2007
Messages
23
Location
Warwick, UK
Programming Experience
10+
Hi All,

I’m having a problem creating a Server Application (in VB.NET) which can be early bound to by two Client Applications and exchange data between them via the Server Application.

Scenario

There are three applications ClientApp1, ClientApp2 & ServerApp all residing on the same server/workstation.

I’ve chosen this approach as ClientApp3 to ClientAppn can be added and removed at any time.

The ClientApps are both manually started and both early bind to the ServerApp.

ClientApp1 sends a message to ClientApp2 via the ServerApp.

ClientApp2 replies to ClientApp1 via the ServerApp.

Do you know how I can create a ServerApp in VB.NET to achieve this?

Every time I create a ServerApp.DLL with a Connect Class in VB.NET the two ClientApps see two different instances of the data in the class. I’ve even created a module with global data read by the Get property but still does not contain the data sent to it by the first ClientApp.

How do I stop multiple instances of the ServerApp being created?:confused:

ServerApp.dll

Module modServerApp
Public gstrAppConnect As String = "AppConnect"
End Module

Public Class clsServerApp

Public Property AppConnect() As String
Get
Return gstrAppConnect
End Get

Set(ByVal value As String)
gstrAppConnect = value
End Set

End Property

End Class

ClientApp.exe

Imports ServerApp.clsServerApp

Public Class frmClientApp
Private objServerApp As New ServerApp.clsServerApp

Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
TextBox1.Text = objServerApp.AppConnect
End Sub

Private Sub btnWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrite.Click
objServerApp.AppConnect = TextBox1.Text
End Sub
End Class

Regards
 
You would need to implement the Singleton pattern. I've only ever done it to restrict to one instance of a type within one application but I'm fairly sure it works across AppDomains too.
VB.NET:
Public Class ServerApp

    Private Shared _instance As ServerApp

    Public Shared ReadOnly Property Instance() As ServerApp
        Get
            If _instance Is Nothing Then
                _instance = New ServerApp
            End If

            Return _instance
        End Get
    End Property

    Private Sub New()
    End Sub

End Class
All interaction with the class occurs via the Instance property, which always returns the same one and only instance.
 
I'm a little new at VB.NET, how to use this singleton.

I've placed the code in my ServerApp what do I have to do with my ClientApps?

How do I use Instance?
 
Last edited:
Like I already said, the Instance property returns the one and only instance of the class. Whenever you want to interact with that one and only instance you get it from the Instance property. For example, instead of this:
VB.NET:
TextBox1.Text = objServerApp.AppConnect
you'd do this:
VB.NET:
TextBox1.Text = ServerApp.Instance.AppConnect
 
I’m having a problem creating a Server Application
The singleton class pattern above only works within one AppDomain. The easiest for you is to use Remoting (with a Singleton SAO) to communicate between appdomains. Search up some tutorials and check the documentation, we also have a Remoting forum here.
 
Warning Produced

Thanks again but the code produces a warning.

VB.NET:
TextBox1.Text = objServerApp.GetInstance.AppConnect

Warning: Accesss of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.

Is there anyway arround it?
 
Still Not Working

I've now tested the suggested ServerApp code with two ClientApps and it is still creating two instances. I think the class is a Singleton but I think the ClientApp create two instances of the DLL's.

I created a ServerApp.DLL with only a singleton class clsServerApp.


VB.NET:
Public Class clsServerApp

    Private mstrStartup As String

    Private Shared myInstance As clsServerApp

    Public Shared ReadOnly Property GetInstance() As clsServerApp
        Get
            If myInstance Is Nothing Then
                myInstance = New clsServerApp
            End If
            Return myInstance
        End Get
    End Property

    Private Sub New()
        mstrStartup = "clsServerApp [Creation Time: " & DateTime.Now.ToString() & "]"
    End Sub

    Public ReadOnly Property Startup() As String
        Get
            Return mstrStartup
        End Get
    End Property

End Class

I then created a new ClientApp1.exe, added the ServerApp.DLL as a reference and the following code.
VB.NET:
Public Class Form1

    Private objServerApp As ServerApp.clsServerApp

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Text = objServerApp.GetInstance.Startup
    End Sub

End Class


I ran ClientApp1.exe:

Result: clsServerApp [Creation Time: 14/11/2007 19:16:25]

I then created a second ClientApp2.exe with the same code as ClientApp1.exe, added the ServerApp.DLL as a reference.

I ran ClientApp2.exe:

Result: clsServerApp [Creation Time: 14/11/2007 19:16:56]

If only one instance of the class is being created how come the times are different?

How do I stop two instances of the DLL being created?:confused:
 
Remoting

The singleton class pattern above only works within one AppDomain. The easiest for you is to use Remoting (with a Singleton SAO) to communicate between appdomains. Search up some tutorials and check the documentation, we also have a Remoting forum here.

I thought Remoting was used to communicate from PC to PC over TCP/IP and as all my applications are on one PC it was overkill. Is there another solution?
 
Each application run in its own appdomain. Remoting can be used to communicate between appdomains at same computer or different computers, different transport layers are available. For local computer an IPC remoting channel is best (can't be used PC to PC), this uses named pipes for transportation and is very fast, it also use binary serialization as formatting which gives little overhead in messaging.

Another option is using sockets.
 
IPC Remoting

Each application run in its own appdomain. Remoting can be used to communicate between appdomains at same computer or different computers, different transport layers are available. For local computer an IPC remoting channel is best (can't be used PC to PC), this uses named pipes for transportation and is very fast, it also use binary serialization as formatting which gives little overhead in messaging.

Another option is using sockets.

Thanks for the reply John,
Yes I think IPC Remoting is the way to go. Do you know of any good articles specifically for VB.NET & IPC Remoting?
 
IPC Remoting

Guy's thanks for all the help,
IPC & TCP Remoting was the way to go. I can now communicate locally on the same PC using IPC Remoting and across the intranet using TCP Remoting. I'm also using a Singleton Class so all Client apps can see the same data. :)
 
Last edited:
Signing a User Control

I've created a basic User Control in VB.NET that works fine in both a Windows Form and a WEB Page.

I'm using the following code to load the User Control into the WEB Page.

HTML:
<object id="MyUserControl" classid="http:MyUserControl.dll#MyUserControl.TimerControl"></object>

When I sign the User Control it stops working in the WEB page but is fine in the Windows Form.

What must I now do to enable the Signed User Control to work in the WEB page?
 
Back
Top