Programmer's Highway

C# Tips and simple code

I'm very sorry for machine translation in English.

C# IPC InterProcess Communication

InterProcess Communication is a mechanism for exchanging information between between several programs.
Because there is a function of the interprocess communication in .NET Framework, and interprocess communication can easily use it.

The communication method can be selected from a different communication system 3 TCP, HTTP, and IPC.
If you want to communicate between processes on the same machine, we use the IPC channel.

This sample code was referring to the pages of MSDN.
http://msdn.microsoft.com/library/en-us/system.runtime.remoting.channels.ipc.ipcchannel.aspx

IPC Server

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
 
namespace IpcSample
{
    class IpcServer
    {
        public IpcRemoteObject RemoteObject { getset; }
    
        public IpcServer()
        {
            // Create the server channel.
            IpcServerChannel channel = new IpcServerChannel("ipcSample");
    
            // Register the server channel.
            ChannelServices.RegisterChannel(channel, true);
    
            // Create an instance of the remote object.
            RemoteObject = new IpcRemoteObject();
            RemotingServices.Marshal(RemoteObject, "test"typeof(IpcRemoteObject));
        }
    }
}

Please add a "System.Runtime.Remoting" in reference to the project configuration.

IPC Client

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
 
namespace IpcSample
{
    class IpcClient
    {
        public IpcRemoteObject RemoteObject { getset; }
    
        public IpcClient()
        {
            // Create the channel.
            IpcClientChannel channel = new IpcClientChannel();
    
            // Register the channel.
            ChannelServices.RegisterChannel(channel, true);
    
            // Get an instance of the remote object.
            RemoteObject = Activator.GetObject(typeof(IpcRemoteObject), "ipc://ipcSample/test"as IpcRemoteObject;
        }
    }
}

Please add a "System.Runtime.Remoting" in reference to the project configuration.

Remote Object

using System;
 
namespace IpcSample
{
    public class IpcRemoteObject : MarshalByRefObject
    {
        public int Counter { getset; }
    }
}

Creates an instance of a server-side program IpcServer to generate a client-side program IpcClient.
While running in separate processes, the properties of each class RemoteObject become such images are sharing the same instance.
Also you can client side is able to generate multiple share a RemoteObject in one-to-many.

Now that it is easier, you may experience problems under is generated in these samples. Also, check the page below.

Visual Studio 2010 .NET Framework 4