Click here to Skip to main content
11,502,845 members (46,587 online)
Click here to Skip to main content

.NET Interprocess Communication Revisited

, 4 Jan 2010 CPOL 61.1K 1.8K 108
Rate this:
Please Sign up or sign in to vote.
The XDMessaging 2.0 library provides an easy-to-use, zero configuration alternative to existing IPC implementations.

Introduction

The XDMessaging library provides an easy-to-use, zero configuration solution to same-box communications. It provides a simple API for broadcasting and receiving messages across application and process boundaries.

Version 1.0 was originally developed to use low-level Windows Messaging and to offer a performant solution for Windows Forms based applications. However, the restriction in using Windows Messages is that it does not provide a way to communicate with non-Forms based applications such as Windows Services which don't have their own message pump.

Version 2.0 now extends the library further, and provides a second file IO based transport mode for messaging. The new IOStream mode can be used to communicate with Windows services, console apps, and continues to work with Forms based applications.

The library allows the use of user-defined pseudo channels through which messages may be sent and received. Any application can send a message to any channel, but it must register as a listener with the channel in order to receive. In this way, developers can quickly and programmatically devise how their applications will communicate with each other best, to work in harmony.

Advantages

The XDMessaging library offers some advantages over other IPC technologies like WCF, .NET Remoting, Sockets, NamedPipes, and MailSlots. To begin with, the library does not require a server-client relationship as there is no physical connection between processes.

With XDMessaging, messages can be broadcast by multiple applications, and instantly received by multiple listeners in a disconnected fashion. MailSlots offer the closest match to this functionality; however, I found this had a few limitations. Although messages are also disconnected, there is no way to peek at a message, and as soon as the message is read, it is removed from the MailSlot. It's great for broadcasting messages, but limited to a single client for receiving the message. The other limitation is that messages are not instant, and requires polling of the MailSlot.

It's also worth noting that most of the existing IPC implementations require the opening of specific ports and a somewhat painful configuration of settings to make them work. With XDMessaging, there is no configuration; the API determines where messages are sent, and which messages are received using pseudo channels. The disadvantage is that communication is limited to a single machine, so for network communication, other IPC technologies such as WCF are much more suited.

Using the Library

To use the library, create an instance of a IXDBroadcast and use this to send a message to a named channel. You can then create an instance of IXDListener to receive messages on a particular channel. The channels are arbitrary strings chosen to represent a channel, and are not case sensitive.

Before creating the broadcast and listener instances, you must first decide which transport mode you want to use for the library. There are two modes as follows, and each has advantages over the other.

Transport Modes

  • IOStream: This uses file based IO to broadcast messages via a shared directory. A FileSystemWatcher is used within listener classes to monitor changes and trigger the MessageReceived event containing the broadcast message. This mode can be used in Windows Services, console applications, and Windows Forms based applications. Channels are created as separate directories on the file system for each channel. The temporary directories should be accessible by all processes, and there should be no need for manual configuration.
  • WindowsMessaging: This uses the WM_COPYDATA Windows message to copy data between applications. The broadcast implementation sends the Windows messages directly to a hidden window on the listener instance, which dispatches the MessageReceived event with the copied data. Channels are created by adding/removing Windows properties. This offers the most performant solution for Windows Forms based applications, but does not work for Windows Services, console apps, or other applications without a message pump.

Note: Messages broadcast using a particular mode can only be read by applications using a listener in the same mode. For example, IOStream listeners cannot read messages broadcast in the WindowsMessaging mode.

See the included sample applications for more information on using the library in Windows Forms applications or within a Windows Service.

Examples

Example: Sending a message in a particular mode:

// Create an instance of IXDBroadcast using IOStream mode
IXDBroadcast broadcast = XDBroadcast.CreateBroadcast(XDTransportMode.IOStream);

// or create an instance of IXDBroadcast using WindowsMessaging mode
IXDBroadcast broadcast = XDBroadcast.CreateBroadcast(XDTransportMode.WindowsMessaging);

// Send shutdown message to a channel named commands
broadcast.SendToChannel("commands", "shutdown");

Example: Listening for messages on a particular channel:

// Create our listener instance using IOStream mode
IXDListener listener = XDListener.CreateListener(XDTransportMode.IOStream);

// or create our listener using WindowsMessaging mode
IXDListener listener = XDListener.CreateListener(XDTransportMode.WindowsMessaging);

// Register channels to listen on
listener.RegisterChannel("events");
listener.RegisterChannel("status");
listener.RegisterChannel("commands");

// Stop listening on a specific channel
listener.UnRegisterChannel("status");

Example: Handling the messages:

// Attach an event handler to a listener instance
listener.MessageReceived+=XDMessageHandler(this.listener_MessageReceived);

// process the message
private void listener_MessageReceived(object sender, XDMessageEventArgs e)
{
 // e.DataGram.Message is the message
 // e.DataGram.Channel is the channel name
 switch(e.DataGram.Message)
 {
  case "shutdown":
             this.Close();
      break;
 }
}

Further Reading

History

  • 12 Dec 2009: Initial release
  • 16 Dec 2009: Optimized performance
  • 4 Jan 2010: Adding network propagation support via MailSlots

License

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

Share

About the Author

TheCodeKing
Architect
United Kingdom United Kingdom
Mike Carlisle - Technical Architect with over 15 years experience in a wide range of technologies.

@TheCodeKing
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 5 Pin
Phil Seeman3-Jul-14 10:48
memberPhil Seeman3-Jul-14 10:48 
QuestionNot bad, but Pin
mlferra4-Mar-14 8:37
membermlferra4-Mar-14 8:37 
AnswerRe: Not bad, but Pin
TheCodeKing4-Mar-14 8:58
memberTheCodeKing4-Mar-14 8:58 
NewsXDMessaging 4.0 Pin
TheCodeKing3-Feb-13 10:24
memberTheCodeKing3-Feb-13 10:24 
GeneralMy vote of 5 Pin
TheFigmo30-Jan-13 6:46
memberTheFigmo30-Jan-13 6:46 
Questionregarding Interprocess Communication Pin
Tridip Bhattacharjee13-Nov-12 19:51
memberTridip Bhattacharjee13-Nov-12 19:51 
AnswerRe: regarding Interprocess Communication Pin
TheCodeKing4-Mar-14 8:58
memberTheCodeKing4-Mar-14 8:58 
QuestionRandom Crash [modified] Pin
stormyseasailor3-Oct-12 23:42
memberstormyseasailor3-Oct-12 23:42 
AnswerRe: Random Crash Pin
TheCodeKing4-Oct-12 11:26
memberTheCodeKing4-Oct-12 11:26 
GeneralRe: Random Crash Pin
stormyseasailor5-Oct-12 23:18
memberstormyseasailor5-Oct-12 23:18 
GeneralMy vote of 5 Pin
sun19915-Sep-12 16:55
membersun19915-Sep-12 16:55 
Very neat! 3.0 is even better!
Thanks.
QuestionBrilliant! Pin
Cleveland Mark Blakemore30-Jan-12 19:56
memberCleveland Mark Blakemore30-Jan-12 19:56 
GeneralRunning in terminal server session: mutex problem Pin
J.Buysrogge17-Feb-11 2:29
memberJ.Buysrogge17-Feb-11 2:29 
GeneralRe: Running in terminal server session: mutex problem Pin
Pika8117-Feb-11 3:46
memberPika8117-Feb-11 3:46 
GeneralRe: Running in terminal server session: mutex problem Pin
TheCodeKing17-Feb-11 11:58
memberTheCodeKing17-Feb-11 11:58 
GeneralRe: Running in terminal server session: mutex problem Pin
TheCodeKing17-Feb-11 11:49
memberTheCodeKing17-Feb-11 11:49 
GeneralRe: Running in terminal server session: mutex problem [modified] Pin
Pika8118-Feb-11 2:09
memberPika8118-Feb-11 2:09 
GeneralRe: Running in terminal server session: mutex problem Pin
TheCodeKing18-Feb-11 3:18
memberTheCodeKing18-Feb-11 3:18 
GeneralRe: Running in terminal server session: mutex problem Pin
Pika8118-Feb-11 3:48
memberPika8118-Feb-11 3:48 
GeneralRe: Running in terminal server session: mutex problem Pin
TheCodeKing25-Feb-11 23:59
memberTheCodeKing25-Feb-11 23:59 
GeneralRe: Running in terminal server session: mutex problem Pin
TheCodeKing26-Feb-11 1:24
memberTheCodeKing26-Feb-11 1:24 
GeneralRe: Running in terminal server session: mutex problem Pin
Pika812-Mar-11 3:56
memberPika812-Mar-11 3:56 
GeneralRe: Running in terminal server session: mutex problem Pin
Pika812-Mar-11 5:18
memberPika812-Mar-11 5:18 
GeneralRe: Running in terminal server session: mutex problem Pin
TheCodeKing3-Mar-11 2:49
memberTheCodeKing3-Mar-11 2:49 
GeneralRe: Running in terminal server session: mutex problem Pin
TheCodeKing5-Mar-11 10:17
memberTheCodeKing5-Mar-11 10:17 
GeneralSupport for WPF Pin
dyna1manning23-Dec-10 8:21
memberdyna1manning23-Dec-10 8:21 
GeneralRe: Support for WPF Pin
TheCodeKing23-Dec-10 11:00
memberTheCodeKing23-Dec-10 11:00 
Generalcode king Pin
Mubi | www.mrmubi.com30-Jul-10 12:36
memberMubi | www.mrmubi.com30-Jul-10 12:36 
QuestionIssue with elevated programs? Pin
demeester10-Apr-10 0:56
memberdemeester10-Apr-10 0:56 
AnswerRe: Issue with elevated programs? Pin
TheCodeKing10-Apr-10 11:14
memberTheCodeKing10-Apr-10 11:14 
GeneralRe: Issue with elevated programs? Pin
demeester11-Apr-10 0:54
memberdemeester11-Apr-10 0:54 
GeneralRe: Issue with elevated programs? Pin
TheCodeKing11-Apr-10 2:01
memberTheCodeKing11-Apr-10 2:01 
GeneralWindows Server 2008 Pin
Member 229267410-Mar-10 5:59
memberMember 229267410-Mar-10 5:59 
GeneralRe: Windows Server 2008 Pin
TheCodeKing10-Mar-10 6:10
memberTheCodeKing10-Mar-10 6:10 
GeneralRe: Windows Server 2008 Pin
Member 229267411-Mar-10 6:13
memberMember 229267411-Mar-10 6:13 
GeneralRe: Windows Server 2008 Pin
TheCodeKing12-Mar-10 9:24
memberTheCodeKing12-Mar-10 9:24 
GeneralAwesome solution Pin
Ondrej Salplachta14-Feb-10 0:52
memberOndrej Salplachta14-Feb-10 0:52 
GeneralAdditional Features & Thoughts Pin
sam.hill1-Jan-10 9:42
membersam.hill1-Jan-10 9:42 
GeneralRe: Additional Features & Thoughts Pin
TheCodeKing1-Jan-10 11:57
memberTheCodeKing1-Jan-10 11:57 
GeneralNew features coming soon... Pin
TheCodeKing16-Dec-09 14:21
memberTheCodeKing16-Dec-09 14:21 
GeneralRe: New features coming soon... Pin
TheCodeKing17-Dec-09 10:38
memberTheCodeKing17-Dec-09 10:38 
GeneralRe: New features coming soon... Pin
sam.hill18-Dec-09 17:04
membersam.hill18-Dec-09 17:04 
GeneralRe: New features coming soon... [modified] Pin
TheCodeKing19-Dec-09 0:02
memberTheCodeKing19-Dec-09 0:02 
GeneralRe: New features coming soon... [modified] Pin
sam.hill19-Dec-09 6:43
membersam.hill19-Dec-09 6:43 
GeneralRe: New features coming soon... Pin
TheCodeKing20-Dec-09 9:18
memberTheCodeKing20-Dec-09 9:18 
GeneralRe: New features coming soon... Pin
sam.hill20-Dec-09 16:33
membersam.hill20-Dec-09 16:33 
GeneralRe: New features coming soon... Pin
TheCodeKing21-Dec-09 7:19
memberTheCodeKing21-Dec-09 7:19 
GeneralRe: New features coming soon... Pin
sam.hill21-Dec-09 18:32
membersam.hill21-Dec-09 18:32 
GeneralRe: New features coming soon... Pin
TheCodeKing21-Dec-09 22:15
memberTheCodeKing21-Dec-09 22:15 
GeneralRe: New features coming soon... Pin
sam.hill22-Dec-09 5:48
membersam.hill22-Dec-09 5:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 4 Jan 2010
Article Copyright 2009 by TheCodeKing
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid