Load remote object : Remoting « Socket Network « VB.Net Tutorial






Load remote object
Imports System.Runtime.Remoting


Imports System.Windows.Forms


<Serializable()> Public Class Hello
    Private strmyDomain As String

    Public Sub New()
        strmyDomain = AppDomain.CurrentDomain.FriendlyName
    End Sub
    Public Function GetMessage() As String
        Return strmyDomain
    End Function
End Class

''''''''''''''''''''''''''''''''''''''''''''''''''''

public class RemoteLoadingForm
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    Inherits System.Windows.Forms.Form

    Public Sub New()
        MyBase.New()

        InitializeComponent()

    End Sub
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    Private components As System.ComponentModel.IContainer

    Friend WithEvents btnCreatDomain As System.Windows.Forms.Button
    Friend WithEvents txtDomain As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents btnLoad As System.Windows.Forms.Button
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents lblMessage As System.Windows.Forms.Label
    Friend WithEvents btnCreateSayHello As System.Windows.Forms.Button
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Label5 As System.Windows.Forms.Label
    Friend WithEvents lblLoad As System.Windows.Forms.Label
    Friend WithEvents lblCreate As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.btnCreatDomain = New System.Windows.Forms.Button()
        Me.txtDomain = New System.Windows.Forms.TextBox()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.btnLoad = New System.Windows.Forms.Button()
        Me.Label2 = New System.Windows.Forms.Label()
        Me.lblMessage = New System.Windows.Forms.Label()
        Me.btnCreateSayHello = New System.Windows.Forms.Button()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.lblLoad = New System.Windows.Forms.Label()
        Me.Label5 = New System.Windows.Forms.Label()
        Me.lblCreate = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        '
        'btnCreatDomain
        '
        Me.btnCreatDomain.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.btnCreatDomain.Location = New System.Drawing.Point(133, 56)
        Me.btnCreatDomain.Name = "btnCreatDomain"
        Me.btnCreatDomain.Size = New System.Drawing.Size(123, 32)
        Me.btnCreatDomain.TabIndex = 5
        Me.btnCreatDomain.Text = "New Domain"
        '
        'txtDomain
        '
        Me.txtDomain.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.txtDomain.Location = New System.Drawing.Point(123, 16)
        Me.txtDomain.Name = "txtDomain"
        Me.txtDomain.Size = New System.Drawing.Size(307, 23)
        Me.txtDomain.TabIndex = 4
        Me.txtDomain.Text = ""
        '
        'Label1
        '
        Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.Label1.Location = New System.Drawing.Point(20, 16)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(103, 24)
        Me.Label1.TabIndex = 3
        Me.Label1.Text = "New Domain"
        '
        'btnLoad
        '
        Me.btnLoad.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.btnLoad.Location = New System.Drawing.Point(266, 56)
        Me.btnLoad.Name = "btnLoad"
        Me.btnLoad.Size = New System.Drawing.Size(123, 32)
        Me.btnLoad.TabIndex = 6
        Me.btnLoad.Text = "Load SayHello"
        '
        'Label2
        '
        Me.Label2.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.Label2.Location = New System.Drawing.Point(10, 112)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(144, 24)
        Me.Label2.TabIndex = 8
        Me.Label2.Text = "Message"
        Me.Label2.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'lblMessage
        '
        Me.lblMessage.BackColor = System.Drawing.Color.White
        Me.lblMessage.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lblMessage.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.lblMessage.Location = New System.Drawing.Point(164, 112)
        Me.lblMessage.Name = "lblMessage"
        Me.lblMessage.Size = New System.Drawing.Size(450, 24)
        Me.lblMessage.TabIndex = 7
        '
        'btnCreateSayHello
        '
        Me.btnCreateSayHello.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.btnCreateSayHello.Location = New System.Drawing.Point(399, 56)
        Me.btnCreateSayHello.Name = "btnCreateSayHello"
        Me.btnCreateSayHello.Size = New System.Drawing.Size(123, 32)
        Me.btnCreateSayHello.TabIndex = 9
        Me.btnCreateSayHello.Text = "Create SayHello"
        '
        'Label3
        '
        Me.Label3.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.Label3.Location = New System.Drawing.Point(0, 144)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(154, 24)
        Me.Label3.TabIndex = 11
        Me.Label3.Text = "Loading Message"
        Me.Label3.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'lblLoad
        '
        Me.lblLoad.BackColor = System.Drawing.Color.White
        Me.lblLoad.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lblLoad.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.lblLoad.Location = New System.Drawing.Point(164, 144)
        Me.lblLoad.Name = "lblLoad"
        Me.lblLoad.Size = New System.Drawing.Size(450, 24)
        Me.lblLoad.TabIndex = 10
        '
        'Label5
        '
        Me.Label5.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.Label5.Location = New System.Drawing.Point(10, 176)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(144, 24)
        Me.Label5.TabIndex = 13
        Me.Label5.Text = "Message"
        Me.Label5.TextAlign = System.Drawing.ContentAlignment.TopRight
        '
        'lblCreate
        '
        Me.lblCreate.BackColor = System.Drawing.Color.White
        Me.lblCreate.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lblCreate.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(136, Byte))
        Me.lblCreate.Location = New System.Drawing.Point(164, 176)
        Me.lblCreate.Name = "lblCreate"
        Me.lblCreate.Size = New System.Drawing.Size(450, 24)
        Me.lblCreate.TabIndex = 12
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(655, 253)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label5, Me.lblCreate, Me.Label3, Me.lblLoad, Me.btnCreateSayHello, Me.Label2, Me.lblMessage, Me.btnLoad, Me.btnCreatDomain, Me.txtDomain, Me.Label1})
        Me.ResumeLayout(False)

    End Sub

    Dim myAppDomain As AppDomain
    Private Sub btnCreatDomain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreatDomain.Click
        Dim strDomain As String
        Try
            strDomain = txtDomain.Text
            myAppDomain = AppDomain.CreateDomain(strDomain)
            lblMessage.Text = "New " + myAppDomain.FriendlyName + " Created"
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        Dim mySayHelloInst As ObjectHandle
        Dim strDomain As String
        Dim mySayHello As Hello

        mySayHelloInst = myAppDomain.CreateInstance("SayHello", "Hello")
        mySayHello = mySayHelloInst.Unwrap()
        lblLoad.Text = "SayHello " + mySayHello.GetMessage()
    End Sub

    Private Sub btnCreateSayHello_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateSayHello.Click

        Dim strDomain As String
        Dim mySayHello As New Hello()

        lblCreate.Text = "SayHello "  + mySayHello.GetMessage()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class








22.21.Remoting
22.21.1.Load remote objectLoad remote object