Click here to Skip to main content
15,897,371 members
Articles / Programming Languages / C#

.NET Interprocess Communication Revisited

Rate me:
Please Sign up or sign in to vote.
4.96/5 (30 votes)
4 Jan 2010CPOL4 min read 122.3K   2.3K   111  
The XDMessaging 2.0 library provides an easy-to-use, zero configuration alternative to existing IPC implementations.
'=============================================================================
'
'	(C) Copyright 2007, Michael Carlisle (mike.carlisle@thecodeking.co.uk)
'
'   http://www.TheCodeKing.co.uk
'  
'	All rights reserved.
'	The code and information is provided "as-is" without waranty of any kind,
'	either expresed or implied. Please do not use commerically without permission.
'
'-----------------------------------------------------------------------------
'	History:
'		25/02/2007	Michael Carlisle				Version 1.0 VB Port
'	    12/12/2009	Michael Carlisle				Version 2.0 VB Port
'=============================================================================
'
Imports System.ComponentModel
Imports TheCodeKing.Net.Messaging
Imports TheCodeKing.Net.Messaging.XDListener

Public Class Messenger
    Private listener As IXDListener
    Private broadcast As IXDBroadcast
    Private Delegate Sub UpdateDisplay(ByVal dataGram As DataGram)

    Protected Overrides Sub OnLoad(ByVal e As EventArgs)
        MyBase.OnLoad(e)
        Me.UpdateDisplayText("Launch multiple instances to demo interprocess communication." & ChrW(13) & ChrW(10), Color.Gray)
        Me.Text = (Me.Text & String.Format("Window Id: {0}", MyBase.Handle))
        Me.listener = XDListener.CreateListener(XDTransportMode.IOStream)
        AddHandler Me.listener.MessageReceived, New XDMessageHandler(AddressOf Me.OnMessageReceived)
        Me.listener.RegisterChannel("Status")
        Me.listener.RegisterChannel("UserMessage")
        Me.broadcast = XDBroadcast.CreateBroadcast(XDTransportMode.IOStream)
        Me.broadcast.SendToChannel("Status", String.Format("Window {0} created!", MyBase.Handle))
    End Sub
    Private Sub OnMessageReceived(ByVal sender As Object, ByVal e As XDMessageEventArgs)
        If MyBase.InvokeRequired Then
            Try
                Dim callback As New UpdateDisplay(AddressOf UpdateDisplayText)
                MyBase.Invoke(callback, e.DataGram)
            Catch
            End Try
        Else
            Me.UpdateDisplayText(e.DataGram)
        End If
    End Sub
    Private Sub UpdateDisplayText(ByVal dataGram As DataGram)
        Dim textColor As Color
        Dim channel As String = dataGram.Channel.ToLower
        If ((Not channel Is Nothing) AndAlso (channel = "status")) Then
            textColor = Color.Green
        Else
            textColor = Color.Blue
        End If
        Dim msg As String = String.Format("{0}: {1}" & ChrW(13) & ChrW(10), dataGram.Channel, dataGram.Message)
        Me.UpdateDisplayText(msg, textColor)
    End Sub
    Private Sub UpdateDisplayText(ByVal message As String, ByVal textColor As Color)
        Me.displayTextBox.AppendText(message)
        Me.displayTextBox.Select(((Me.displayTextBox.Text.Length - message.Length) + 1), Me.displayTextBox.Text.Length)
        Me.displayTextBox.SelectionColor = textColor
        Me.displayTextBox.Select(Me.displayTextBox.Text.Length, Me.displayTextBox.Text.Length)
        Me.displayTextBox.ScrollToCaret()
    End Sub
    Private Sub sendBtn_Click(ByVal sender As Object, ByVal e As EventArgs) Handles sendBtn.Click
        If (Me.inputTextBox.Text.Length > 0) Then
            Me.broadcast.SendToChannel("UserMessage", String.Format("{0}: {1}", MyBase.Handle, Me.inputTextBox.Text))
            Me.inputTextBox.Text = ""
        End If
    End Sub

    Private Sub statusCheckBox_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles statusCheckBox.Click
        If Me.statusCheckBox.Checked Then
            Me.listener.RegisterChannel("Status")
            Me.broadcast.SendToChannel("Status", String.Format("{0}: Registering for Status.", MyBase.Handle))
        Else
            Me.listener.UnRegisterChannel("Status")
            Me.broadcast.SendToChannel("Status", String.Format("{0}: UnRegistering for Status.", MyBase.Handle))
        End If
    End Sub
    Private Sub msgCheckBox_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles msgCheckBox.Click
        If Me.msgCheckBox.Checked Then
            Me.listener.RegisterChannel("UserMessage")
            Me.broadcast.SendToChannel("Status", String.Format("{0}: Registering for UserMessage.", MyBase.Handle))
        Else
            Me.listener.UnRegisterChannel("UserMessage")
            Me.broadcast.SendToChannel("Status", String.Format("{0}: UnRegistering for UserMessage.", MyBase.Handle))
        End If
    End Sub
    Protected Overrides Sub OnClosing(ByVal e As CancelEventArgs)
        MyBase.OnClosing(e)
        Me.broadcast.SendToChannel("Status", String.Format("Window {0} closing!", MyBase.Handle))
    End Sub
    Protected Overrides Function ProcessCmdKey(ByRef m As Message, ByVal k As Keys) As Boolean
        If ((m.Msg = &H100) AndAlso (k = Keys.Return)) Then
            If (Me.inputTextBox.Text.Length > 0) Then
                Me.broadcast.SendToChannel("UserMessage", String.Format("{0}: {1}", MyBase.Handle, Me.inputTextBox.Text))
                Me.inputTextBox.Text = ""
            End If
            Return True
        End If
        Return MyBase.ProcessCmdKey((m), k)
    End Function

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United Kingdom United Kingdom
Mike Carlisle - Technical Architect with over 20 years experience in a wide range of technologies.

@TheCodeKing

Comments and Discussions