Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / C#
Tip/Trick

Simple Inter-process Communication system for .NET framework

Rate me:
Please Sign up or sign in to vote.
4.77/5 (9 votes)
12 Mar 2014CPOL2 min read 55.5K   23   9
This article introduces tiny library, that allows easily use IPC with C# (share objects, send messages and exchange data in few lines of code).

Introduction

This article introduces tiny library, that allows easily use IPC with C# (share objects, send messages and exchange data in few lines of code). Simple interface makes entry threshold very low even for beginners.

Background

On my first work place I've faced with such a term as Inter-Process Communication. There was high-level library, implemented with Delphi, which provided easy communication between several process via PID's, window handles, etc. Implementation of that library was quite difficult, and not plain for understanding (it used file mapping and windows messaging API). After I've switched to C#, I became interested whether there are any simple high-level API for IPC in C#.

Using the code

Basic solution is to use IpcChannel. In this way to share one object between 2 processes developer should write something like this code.

Server:

C#
IpcChannel serverChannel = new IpcChannel("MyServerChannel");
ChannelServices.RegisterChannel(serverChannel);
RemotingConfiguration.RegisterWellKnownServiceType(
  typeof(IPCChannelRemoting.MyRemoteObject),
  "SharedObj", WellKnownObjectMode.SingleCall); serverChannel = new IpcChannel("MyServerChannel");IpcChannel 
ChannelServices.RegisterChannel(serverChannel); 
RemotingConfiguration.RegisterWellKnownServiceType(
  typeof(IPCChannelRemoting.MyRemoteObject),
  "SharedObj", WellKnownObjectMode.SingleCall);   

Client:

C#
IpcChannel clientChanel = new IpcChannel("myClient");
ChannelServices.RegisterChannel(clientChanel); 
ISharedObj obj = (Remoteable.ISharedAssemblyInterface)Activator.GetObject(
  typeof(Remoteable.ISharedAssemblyInterface),
  "ipc://MyServerChannel/SharedObj "); 

But what if data exchange is used ubiquitously through application? In this case developer should duplicate such a code everywhere and stipulate rules to create URI for shared objects.

But what if developer need not just request data from another process, but subscribe for some signal/event from it? Of course, he can create some Thread/Timer to check it in cycle. Obviously he can't manage with it without some king of self-created architecture.

As I know from previous experience in developing application that uses IPC technology, it's very convenient to use and maintain following approach:

  • there are three entities: sender, receiver and message;
  • each receiver/sender has it's own unique ID, which is used for messages dispatching. For this goal developer can use, for example, application PIDs, window handles or any well-known identifier for each side of data exchange.
  • message - is abstract entity, that can contain property of any type. It can be synchronous or asynchronous. The difference between synchronous and asynchronous messages is that application, which receives message can put response in the same message (if it is necessary), and response will be accessible for dispatcher at once after message processing (on stack return). Applications, that send and receive such messages, should be aware of particular descendants of base message class.

    Eventually, with this system, data exchange between processes is reduced to the following steps:

  • create 'dispatcher' object and 'message' object;
  • dispatch message to desired receiver;
  • process message within receiver.

As you could notice, this approach is very similar to native Windows Api (postmessage/sendmessage/wndproc), but it supports OOP, events, easy passing real objects between processes, without deep knowledge of Inter process communication. Example:

C#
using (BaseIPCDispatcher dispatcher = new BaseIPCDispatcher(slaveReceaverGUID)) {
  TestAsyncComplexMessage testMessage = new TestAsyncComplexMessage (ReceaverID, null);
  dispatcher.Dispatch(testMessage);
}

Subscription for this message in another (or the same) process:

C#
…
Receiver.OnReceaveIPCMessage += OnReceaveMessage;
…
private void OnReceaveMessage(object sender, ReceaveMessageEventArgs e) {
  TestAsyncComplexMessage testAsyncMessage = e.Message as TestAsyncComplexMessage;
  if (testAsyncMessage != null) {
    // process message
  }
}

All code covered with functional tests using master/slave architecture. Here is the code. It as available also on https://github.com/perevernihata/SimpleIPCCommSystem .

Points of Interest

It made me sad, that such a powerful and high-level language as C# doesn't allow developer to share objects and exchange data between processes easily. At the same time it was a challenge for me to remake self-made Delphi IPC system in C#.

History

Version 1. (TODAY)

Version 2. Added direct link to the code sources.

This article was originally posted at http://habrahabr.ru/post/187172

License

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


Written By
Software Developer (Senior) MYOB
New Zealand New Zealand
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionIPCUri receaverUri Pin
Member 1043556820-Jul-15 21:37
Member 1043556820-Jul-15 21:37 
AnswerRe: IPCUri receaverUri Pin
Ivan Perevernykhata20-Jul-15 21:49
Ivan Perevernykhata20-Jul-15 21:49 
GeneralRe: IPCUri receaverUri Pin
Member 1043556821-Jul-15 23:57
Member 1043556821-Jul-15 23:57 
AnswerRe: IPCUri receaverUri Pin
Ivan Perevernykhata22-Jul-15 0:04
Ivan Perevernykhata22-Jul-15 0:04 
Questioncan we send mouse motion ? Pin
Omarkkk5-Mar-15 23:44
Omarkkk5-Mar-15 23:44 
AnswerRe: can we send mouse motion ? Pin
Ivan Perevernykhata6-Mar-15 5:50
Ivan Perevernykhata6-Mar-15 5:50 
QuestionLooks great.. but, where is the code? Pin
aquila_prosperus13-Mar-14 1:01
aquila_prosperus13-Mar-14 1:01 
AnswerRe: Looks great.. but, where is the code? Pin
Ivan Perevernykhata13-Mar-14 1:08
Ivan Perevernykhata13-Mar-14 1:08 
QuestionNice! Pin
Volynsky Alex12-Mar-14 2:26
professionalVolynsky Alex12-Mar-14 2:26 

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

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