PblicHieip Hag Ana February 2016

Inter-process communication sample-server & client C#

Using .NET 4, wpf c#, I am passing method return values and parameters between two processes.

As I need the connection open and active a all times, I have tried my best to minimize the code that is recurring (within the loop) but unless I put this whole code inside the loop it did not succeed (after the first transfer the connection to server was closed), so as it is here, it does work repeatedly.

I was wondering first is this the way it should be coded, all the process including the new instance, dispose, close... within the loop?

Is the only available datatype for inter-process communication to pass as a string (inefficient)?

public void client()
{
    for (int i = 0; i < 2; i++)
    {
        System.IO.Pipes.NamedPipeClientStream pipeClient = 
            new System.IO.Pipes.NamedPipeClientStream(".", "testpipe", 
                          System.IO.Pipes.PipeDirection.InOut, System.IO.Pipes.PipeOptions.None);

        if (pipeClient.IsConnected != true) 
        {
            pipeClient.Connect(550); 
        }

        System.IO.StreamReader sr = new System.IO.StreamReader(pipeClient);
        System.IO.StreamWriter sw = new System.IO.StreamWriter(pipeClient);

        string status;
        status = sr.ReadLine();

        if (status == "Waiting")
        {
           try
           {
              sw.WriteLine("param1fileName.cs,33" + i);
              sw.Flush();
              pipeClient.Close();
           }
           catch (Exception ex) { throw ex; }
        }
    }
}


public string server()
{
    NamedPipeServerStream pipeServer = null;
    do
    {
        try
        {
            pipeServer = new NamedPipeServerStream("testpipe", PipeDirection.InOut, 4);

            StreamReader sr = new StreamReader(pipeServer);
            StreamWriter sw = new StreamWriter(pipeServer);

            System.Threading.Thread.Sleep(100);
            pipeServer.WaitForConnection();
            string test;
            sw        

Answers


PblicHieip Hag Ana February 2016

after a thorough research on inter process communication I have changed the approach from using Named pipes to Memory mapped files, as it is all rounds winner, no need to recreate it and faster

I present the ultimate partitioned-global-app-inter-process communication

any thoughts on the code will be greatly appreciated!

public class MMFinterComT
{
    public EventWaitHandle flagCaller1, flagCaller2, flagReciver1, flagReciver2;

    private System.IO.MemoryMappedFiles.MemoryMappedFile mmf;
    private System.IO.MemoryMappedFiles.MemoryMappedViewAccessor accessor;

    public virtual string DepositChlName { get; set; }
    public virtual string DepositThrdName { get; set; }
    public virtual int DepositSize { get; set; }
    private System.Threading.Thread writerThread;
    private bool writerThreadRunning;


    public int ReadPosition { get; set; }
    public List<string> statusSet;
    private int writePosition;
    public int WritePosition
    {
        get { return writePosition; }
        set
        {
            if (value != writePosition)
            {
                this.writePosition = value;
                this.accessor.Write(WritePosition + READ_CONFIRM_OFFSET, true);
            }
        }
    }

    private List<byte[]> dataToSend;

    private const int DATA_AVAILABLE_OFFSET = 0;
    private const int READ_CONFIRM_OFFSET = DATA_AVAILABLE_OFFSET + 1;
    private const int DATA_LENGTH_OFFSET = READ_CONFIRM_OFFSET + 1;
    private const int DATA_OFFSET = DATA_LENGTH_OFFSET + 10;
    public IpcMMFinterComSF.MMFinterComTStatus IntercomStatus;
    public MMFinterComT(string ctrIpcChannelNameStr, string ctrIpcThreadName, int ctrMMFSize)
    {
        this.DepositChlName = ctrIpcChannelNameStr;
        this.Deposit Size = ctrMMFSize;
        this.DepositThrdName = ctrIpcThreadName;
        mmf = MemoryMappedFile.CreateOrOpen(DepositChlName, DepositSize);
        accessor = mmf.CreateViewAccessor(0, DepositSiz 

Post Status

Asked in February 2016
Viewed 2,954 times
Voted 9
Answered 1 times

Search




Leave an answer